PHP Login System With MD5 Hashing [MySQL & PHP] [SESSIONS]
First we will need to create a user's table
MYSQL Query:
Now that we got the database made lets go to the config file.
PHP: [config.php]
Now we will move on to the register page
PHP: [register.php]
Ok now lets move on to the login
PHP: [login.php]
First we will need to create a user's table
MYSQL Query:
Code:
CREATE TABLE `user` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `username` TEXT NOT NULL , `password` TEXT NOT NULL ) ENGINE = MYISAM
Now that we got the database made lets go to the config file.
PHP: [config.php]
PHP Code:
<?php
$host = "localhost"; //The Location of your MySQL server [Usually localhost]
$user = "root"; //The username to connect with
$pass = "pass"; //The password for the username
$data = "users"; //The name of the database
//DO NOT TOUCH ANYTHING BELOW
mysql_connect("$host", "$user", "$pass");
mysql_select_db("$data");
SESSION_START();
?>
Now we will move on to the register page
PHP: [register.php]
PHP Code:
<?php
//We have to get a connection
require("config.php");
if(!$_GET['act']){ //This checks ?act= in the URL to see if there is a registration being processed
//Start Making the form
echo("<form method='POST' action='$PHP_SELF?act=register'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<input type='submit' value='Register!' />
</form");
//Now that the form is taken care of
} else if($_GET['act'] == "register"){
//Lets check to make sure that no other users are registered as that username
$check = mysql_query("SELECT * FROM `users` WHERE `username` = '{$_POST['username']}'");
if(mysql_num_rows($check) > 0){
//there is a user already registered
echo("That username is already taken!");
} else {
//There isn't a username
mysql_query("INSERT INTO `user` (`id` ,`username` ,`password`) VALUES (NULL , '{$_POST['username']}', MD5( '{$_POST['password']}' ))");
echo("You have been registered!");
}
?>
Ok now lets move on to the login
PHP: [login.php]
PHP Code:
<?php
//We have to get a connection
require("config.php");
if(!$_GET['act']){ //This checks ?act= in the URL to see if there is a login being processed
//Start Making the form
echo("<form method='POST' action='$PHP_SELF?act=login'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
<input type='submit' value='Login!' />
</form");
//Now that the form is taken care of
} else if($_GET['act'] == "login"){
//check the user details
$check = mysql_query("SELECT * FROM `users` WHERE `username` = '{$_POST['username']}' AND `password` = 'md5($_POST['password'])'");
if(mysql_num_rowS($check) > 0){
//The user had the correct login details
echo("You have been logged in.");
$_SESSION['loggedin'] = "true";
$_SESSION['username'] = $_POST['username'];
} else {
//There was an error
echo("The username/password was not found.");
}
?>
Comment