Step 1. We have to create 4 php files for testing our code.
1.1. index.php
1.2. login_check.php
1.3. successful.php
1.4. logout.php
Step 2. Now I am going to create the database table named as “user_data” in to the database “ewa_testing_environment”.
Don’t know how to create database, click here
Below is the process how to create the database table
CREATE TABLE `user_data` ( `id` int(1) NOT NULL auto_increment, `userId` varchar(200) NOT NULL default '', `user_password` varchar(200) NOT NULL default '', `user_email` varchar(200) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=1; -- -- Dumping data for table `user_data` -- INSERT INTO `user_data` VALUES (1, 'expertwebadvisor', 'ewa@test','info@expertwebadvisor.com');
Step 3. Now create the index.php file and insert the code below
<table width="350" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000"> <tr> <form name="login" method="post" action="login_check.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#fff"> <tr> <td colspan="3"><strong>User Login </strong></td> </tr> <tr> <td>Username</td> <td>:</td> <td><input name="username" type="text" id="username"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="pass" type="text" id="pass"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </form> </tr> </table>
It will look like as the Screenshot given below:
Step 4. When a user submit their credentials i.e. username and password, then it will redirects it on the login_check.php page because I have putted the login_check.php into the form action and on this file there will be the code to check that this user enters the valid information and it is exist in our database or not. Whole code of the login_check.php is given below:
<?php $host_name="localhost"; // Hostname $user_name=""; // Mysql Username $password=""; // Mysql Password $dbname="ewa_testing_environment"; // Database name mysql_connect("$host_name", "$user_name", "$password")or die("cannot connect"); mysql_select_db("$dbname")or die("cannot select DB"); // Fetching the username and password sent from form $username=$_POST['username']; $password=$_POST['pass']; $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $query="SELECT * FROM user_data WHERE userId ='$username' and user_password ='$password'"; $result=mysql_query($query); $count=mysql_num_rows($result); // If result matched table row must be 1; if($count==1){ session_register("username"); session_register("password"); header("location:successful.php"); } else { echo "Please check User Name/Password is wrong"; } ?>
Step 6. If provided User Name, Password is write then it will redirect user on the successful.php page other wise it will be on the same page.
<?php session_start(); if(!session_is_registered(username)){ header("location:index.php"); } else { echo "Welcome On Admin Panel"; } ?>
Step 7. Now you have successfully Logged In. Now here is the process to create the Logout link
and the code of the logout.php file is given below:
<?php session_start(); unset($_SESSION["username"]); header("location: index.php"); ?>
You have done with the Login and Logout in PHP, Enjoy the PHP coding.