For Previous chapter we learn how to create login-page-using-session-without-stored-details-in-database, Here We go to lear how we use login page with session using mysql database
php code for login page with database
First you have a knowledge in mysql with CURD(Create, Update, Read, Delete) Syntax. If you had then Go.
Step 1:
Run Xampp Server, Create Database ‘Developers’ in Localhost/phpmydamin.
step 2:
Create table (copy and paste )
CREATE TABLE IF NOT EXISTS `admin` ( `Sid` int(11) NOT NULL, `User` varchar(20) NOT NULL, `email` varchar(30) NOT NULL, `Password` varchar(20) NOT NULL, `phone` varchar(20) NOT NULL, `Aid` int(11) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; // create table INSERT INTO `admin` (`Sid`, `User`, `email`, `Password`, `phone`, `Aid`) VALUES (1, 'Admin', 'developerdesks@gmail.com', 'admin@123', '9876543210', 1); // values inserted in db
Step 3:
Create Config file named as config.php
<?php $mysql_hostname = "localhost"; $mysql_user = "root"; // default username in localhost is "root" $mysql_password = ""; //password as null $mysql_database = "developer"; // Db name $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database"); mysql_select_db($mysql_database, $bd) or die("Could not select database"); ?>
Step 4:
Create login page and named as Index.php
<?php session_start(); // Session start include 'config.php'; // Config for database connection if(isset($_POST['check'])) // Check form submit with IF Isset function { $username =$_POST['username']; // set username to variable $password =$_POST['password']; //set password to variable $result = mysql_query("SELECT * FROM `admin` WHERE `User`=’$username’ AND Password=’$password'"); //check from database if(mysql_num_rows ($result) > 0 ) { $_SESSION['username'] = $username; // set session name $_SESSION['password'] = $password; header('location:mainpage.php'); } else { $err="Authentication Failed Try again!"; } } ?> <html> <head> <title>Main Page</title> </head> <body> <Center> <?php if(isset($err)){ echo $err; } ?> <!– Print Error –> <form method="POST" name="loginauth" target="_self"> User Name: <input name="username" size="20" type="text" /> <br/><br/> Pass Word: <input name="password" size="20" type="password" /> <br/><br/> <input name="check" type="submit" value="Authenticate" /> </form> </center> </body> </html>
Step 5:
Create mainpage.php
Code
<?php session_start(); if((!isset ($_SESSION['username']) == true) and (!isset ($_SESSION['password']) == true)) // check session condition { unset($_SESSION['username']); unset($_SESSION['password']); header('location:index.php'); } $logged = $_SESSION['username']; ?> <html> <head> </head> <body> <center> <h2>Welcome to Main page</h2><h3> Here we go with <a href="Secondpage.php">Second page </a></h3> <a href=”logout.php”>logout</a> </center> </body> </html>
Explanation
This index.php page will set username and password as “Admin” and “admin@123”.
Check whether given user name and password is correct or wrong.
If the given username and password is correct. Set $_SESSION variable as username and redirect to mainpage.php
HINT : We can Use many number of pages with session variables.
<?php session_start(); if((!isset ($_SESSION['username']) == true) and (!isset ($_SESSION['password']) == true)) { unset($_SESSION['username']); unset($_SESSION['password']); header('location:login.php'); } $logged = $_SESSION['username']; ?>
simply place this in the top of the page.
Step 6:
Create logout.php page for destroy the session or unset the session
<?php session_start(); if(!isset($_SESSION['username'])) { header('location:index.php'); } unset($_SESSION['username']); session_destroy(); header('location:login.php'); ?>
Explanation
When you click Logout.php its check the session and unset the variable values and destroy the session
Step 7 :
Create Secondpage.php
Code
<?php session_start(); if((!isset ($_SESSION['username']) == true) and (!isset ($_SESSION['password']) == true)) { unset($_SESSION['username']); unset($_SESSION['password']); header('location:login.php'); } $logged = $_SESSION['username']; ?> <html> <head> </head> <body> <center> <h2>Welcome to Second page</h2> <p>This page is the second page having session name "<?php echo $logged; ?>" <a href=”logout.php”>logout</a> </center> </body> </html>
Explanation:
The Secondpage.php is the another page with same session. It is used to learn how the session stored the variables in multiple page
This is the simple login form with stored and retrived session data’s from server database.
Enjoy the code.
Php session with database – Full code download here