Show DevBest one (two) page login system!

Status
Not open for further replies.

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
Farley simple, so let's start:
Config.php
PHP:
<?
 
# Getting current page, denieing direct access #
$uri = $_SERVER['PHP_SELF'];
 
if($uri == 'config.php') {
    echo 'direct access is denied!';
} else {
# Config #
$database['MySQL']['Hostname'] = 'localhost'; # Database host #
$database['MySQL']['Username'] = 'root'; # Database username #
$database['MySQL']['Password'] = 'pass123';  # Database password #
$database['MySQL']['Database'] = 'user'; # Database name #
 
# Don't touch! #
 
# Connectining to MySQL Server #
$connect = mysql_connect($database['MySQL']['Hostname'], $database['MySQL']['Username'], $database['MySQL']['Password']);
 
# Checking if connection into is correct #
if(!$connect) {
    die(mysql_error());
}
 
# Cnnnecting to the MySQL Database #
mysql_select_db($database['MySQL']['Database'], $connect);
 
        function secure($string) { return mysql_real_escape_string(stripslashes(htmlspecialchars($string))); }
 
        
}
?>

You can name this one anything you want, make sure it's in the same path as config.php
PHP:
<html>
    <head>
        <title>Page Direct</title>
    </head>
    <body>
<?php
 
# Starting page direct #
include('config.php');
error_reporting('0');
$page = $_GET['page'];
 
if(!$page)  { # If no page is specified, show the index! #
    session_start();
    if(isset($_SESSION['loggedIn'])) {
        header(".$URI.?page=login");
    }
    print "<form method='POST' action=".$URI."?page=login >Username: <input type='text' name='user' /><br /> Password: <input type='password' name='pass' /><br /><input type='Submit' name='sumbit' value='Login' /></form>";
} elseif ($page == 'login') {
         require_once('config.php');
 
            if(isset($_POST['sumbit'])) {
                 # Preventing exploits buy using the secure function #
                $username = secure($_POST['user']);
                $password = secure($_POST['pass']);
 
                $query = mysql_query("SELECT password FROM users WHERE username = '".$username."'");
 
                # Checking the database if there is a result that has the username form the POST value #
                if(mysql_num_rows($query) < 1) {
                die('The username you have entered is not yet registered.');
            }
 
            # If there is a username registered with the value from the POST param... #
            if(mysql_num_rows($query) > 0) {
                 if(mysql_result($query, 0) != $password) {
                    echo mysql_result($query, 0);
                    echo '<br />'.$password;
             }
             elseif(mysql_result($query, 0) == $password) {
                session_start();
                $_SESSION['Username'] = $username;
                $_SESSION['loggedIn'] = true;
                echo "Welcome <b>".$username."</b>. Go ahead and click <b><a href=".$URI."?page=logout>HERE</a></b> to logout.";
             }
        }
    }
} elseif($page == 'logout') {
    session_start();
    if(isset($_SESSION['loggedIn'])) { # Checking if there is an active session # 
        session_destroy(); # Destroys the active session #
        header(".$URI."); # Redirects user to index page #
        echo 'You have logged out';
    } else {
        echo "You're not logged in!";
    }
} else {
    echo '404 not found!'; 
}
# End Code #
?>
        
</body>
</html>

Run this in your database:
Code:
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(25) NOT NULL,
  `password` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Lines that need to be edited:
10-13 on config.php


Credits:
80% _DucklingX (Chris) For making the actual login system.
20% Mace (Me) Making the logout work, making it on page, and releasing it.


Hope you found this useful :)
 
Status
Not open for further replies.

Users who are viewing this thread

Top