PHP Login Form or PHP Login System
If you know how to create website in php and you started creating any php web site that allow user or admin to login then first thing comes in your mind is to create php login script or login form in php that allow user or admin to get login. For login the user must have their respective username and password which he enters in to login form page. To make his successful login we verified his username and password with MySQL database table which contain the username and password for all.
In this tutorial of php point we will show you step by step creation of your first php login form or php script for login. Basically we create one simple login form in html and connect this code with MySQL database and php script.
You can also use the JavaScript validation or jquery validation for your login php form.
Logic:-
1 – First we make signup or registration form to create username and password for login.
In Signup process we check if entered username is already existing in our database then we prompt error message else allow him to create an account.
2 - Now the Second step is creating your Login Page.
If user is registered then allow him to login into home page else prompt error message.
3 - If User is registered user i.e. username password exists in database then before redirecting him into home page we save his username in SESSION.
We use this SESSION variable in our home page to check whether user is logged in or not? If it is session variable is set then allow him to access the content of home page else redirect to login page.
4 - Last step is adding logout button. When user click on logout button we will unset or clear the session variable and redirect him to login page.
Files:-
1 - signup.php (Used for signup or registration process)
2 - login.php (Used for user login)
3 - home.php (User home page after login)
4 - logout.php (Used for logout purpose)
5 - config.php (Contain database connection code)
6 - style.php (For styling the pages)
Step 1:- Create a table "users" with the code below:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(10) NOT NULL,
`lastname` varchar(20) NOT NULL,
`username` varchar(16) DEFAULT NULL,
`password` char(50) DEFAULT NULL,
`email` varchar(20) NOT NULL,
`is_active` int(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
Step 2:- Create a config.php file and put below code in it.
<?php
$database = "mydata"; // the name of the database.
$server = "localhost"; // server to connect to.
$db_user = "root"; // mysql username to access the database with.
$db_pass = ""; // mysql password to access the database with.
$table = "users"; // the table that this script will set up and use.
$link = mysql_connect($server, $db_user, $db_pass);
mysql_select_db($database, $link);
?>
<?php
include("config.php"); //including config.php in our file
//
// Now checking user name and password is entered or not.
if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['email'])) {
$first_name = mysql_real_escape_string($_POST['firstname']);
$last_name = mysql_real_escape_string($_POST['lastname']);
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$password = mysql_real_escape_string(stripslashes(md5($_POST['password'])));
$mail = mysql_real_escape_string($_POST['email']);
$check = "SELECT * from users where username = '" . $user . "'";
$qry = mysql_query($check);
$num_rows = mysql_num_rows($qry);
if ($num_rows > 0) { // Here we are checking if username is already exist or not.
echo "The username you have entered is already exist. Please try another username.";
echo '<a href="signup.php">Try Again</a>';
exit;
}
// Now inserting record in database.
$query = "INSERT INTO users (firstname,lastname,username,password,email,is_active) VALUES ('" . $first_name . "','" . $last_name . "','" . $username . "','" . $password . "','" . $mail . "','1');";
mysql_query($query);
echo "Thank You for Registration.";
echo '<a href="login.php">Click Here</a> to login you account.';
exit;
}
?>
<html>
<head>
<title>Registration Page | Simple login form</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="containt" align="center">
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" class="form-signup">
<div id="header"><h2 class="sansserif">Create an account</h2></div>
<table>
<tr>
<td>Select Your Firstname:</td>
<td> <input type="text" name="firstname" size="20" placeholder="First name"><span class="required">*</span></td>
</tr>
<tr>
<td>Select Your Lastname:</td>
<td> <input type="text" name="lastname" size="20" placeholder="Last name"><span class="required">*</span></td>
</tr>
<tr>
<td>Select Your Username:</td>
<td> <input type="text" name="username" size="20" placeholder="User name"><span class="required">*</span></td>
</tr>
<tr>
<td>Select Your Password:</td>
<td><input type="password" name="password" size="20" placeholder="Password"><span class="required">*</span></td>
</tr>
<tr>
<td>Select Your Email:</td>
<td> <input type="text" name="email" size="20" placeholder="Email"><span class="required">*</span></td>
</tr>
<tr>
<td><input type="submit" value="Sign Up" class="btn btn-large btn-primary"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<?php
if (isset($_POST) && !empty($_POST)) {
session_start();
include("config.php"); //including config.php in our file
$username = mysql_real_escape_string(stripslashes($_POST['username'])); //Storing username in $username variable.
$password = mysql_real_escape_string(stripslashes(md5($_POST['password']))); //Storing password in $password variable.
$match = "select id from $table where username = '" . $username . "' and password = '" . $password . "';";
$qry = mysql_query($match);
$num_rows = mysql_num_rows($qry);
if ($num_rows <= 0) {
echo "Sorry, there is no username $username with the specified password.";
echo "Try again";
exit;
} else {
$_SESSION['user'] = $_POST["username"];
header("location:home.php"); // It is the page where you want to redirect user after login.
}
} else {
?>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="container login">
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" class="form-signin" id = "login_form" >
<h2 class="form-signin-heading">Admin/Employee Login</h2>
<input type="text" name="username" size="20" placeholder="Username">
<input type="password" name="password" size="20" placeholder="Password"></br>
<input type="submit" value="Log In" class="btn btn-large btn-primary">
<a href="signup.php">Sign Up</a>
</form>
</div>
</body>
</html>
<?php
}
?>
Step 5:- Create "home.php" file with following code.
<?php
session_start();
if (isset($_SESSION['user']) && !empty($_SESSION['user'])) {
echo "You are Welcome " . $_SESSION['user'];
?>
<a href = "logout.php">Logout</a>
<?php
} else {
header("location:login.php");
}
?>
Step 6:- Create "logout.php" file with following code.
<?php
session_start();
session_destroy();
header("location:login.php");
?>
Now your PHP login form is ready to use.
Hope this php tutorial is useful for you. Keep following Php Point for more help.