- First we make a login page in html.
- The we create a database in phpAdmin.
- Then through query we used to access the database.
First make an html page:
PAGE :-home.php
<form name="frm" method="POST"> <table> <tr> <td>Login ID</td> <td><input type="text" name="LoginId" /></td> </tr> <tr> <td>Password</td> <td><input type="password" name="Password" /></td> </tr> <tr> <td><input type="checkbox" name="rememberme" />Remember me!</td> <td><input type="submit" value="Login" /></td> </tr> </table> </form>
Save that page with home.php extension and the output of page when we run on browser is:-
Then we create database in phpmyadmin
The name of database is xyz in this we create a table user have values UID, LoginId, Password and Address.
Then we create a php page that get data from html form and compare it to the database the coding of page is describe below save that page with match.php.
PAGE: - match.php
<?php ob_start(); session_start(); $a= $_POST['LoginId']; $b= $_POST['Password']; $c=$_POST['rememberme']; $encrypted_mypassword= $b; $con=mysql_connect("localhost","root",""); if(!$con){ die('Cound'.mysql_error()); } mysql_select_db("xyz",$con); $result= mysql_query("select * from user where LoginId='$a' and Password='$encrypted_mypassword'"); $row= mysql_fetch_array($result); if($row){ $_SESSION['LoginId']= $a; if (isset($a) && isset($encrypted_mypassword)) { if (($a) && ($encrypted_mypassword)){ if (isset($c)) { /* Set cookie to last 1 year */ setcookie('LoginId', $a, time()+60*60*24*365); setcookie('Password', md5($encrypted_mypassword), time()+60*60*24*365); } else { /* Cookie expires when browser closes */ setcookie('LoginId', $a, false); setcookie('Password', md5($encrypted_mypassword), false); } header('Location: next.php'); } else { echo "Username/Password Invalid"; } } else { echo "You must supply a username and password."; } } else { include("home.php"); } mysql_close($con); ?>
When we click on login box we reach on next.php page the codding of that page is describe below:-
PAGE:- next.php
<?php ob_start(); session_start(); echo $_SESSION['LoginId']; if(isset($_SESSION['LoginId'])){ echo "You were already logged in ".$_SESSION['LoginId']."."; echo "<a href="/p.php?id=.$_SESSION['LoginId'].">Click Here</a>"; }else{ header("location: home.php"); } ?>
Then we create a php page that get data from html form and compare it to the database the coding of page is describe below save that page with match.php.
PAGE: - match.php
<?php ob_start(); session_start(); $a= $_POST['LoginId']; $b= $_POST['Password']; $c=$_POST['rememberme']; $encrypted_mypassword= $b; $con=mysql_connect("localhost","root",""); if(!$con){ die('Cound'.mysql_error()); } mysql_select_db("xyz",$con); $result= mysql_query("select * from user where LoginId='$a' and Password='$encrypted_mypassword'"); $row= mysql_fetch_array($result); if($row){ $_SESSION['LoginId']= $a; if (isset($a) && isset($encrypted_mypassword)) { if (($a) && ($encrypted_mypassword)) { if (isset($c)) { /* Set cookie to last 1 year */ setcookie('LoginId', $a, time()+60*60*24*365); setcookie('Password', md5($encrypted_mypassword), time()+60*60*24*365); } else { /* Cookie expires when browser closes */ setcookie('LoginId', $a, false); setcookie('Password', md5($encrypted_mypassword), false); } header('Location: next.php'); } else { echo "Username/Password Invalid"; } } else { echo "You must supply a username and password."; } } else { include("home.php"); } mysql_close($con); ?>
As we pass session value to that page and we print session value to that page so it will print session value 001. And we pass the hyperlink of another page through click here. And also pass the session value and cookies value through query string to another page i.e. p.php. as we encrypt password with md5 algorithm so it will pass its encrypted value through query string.
PAGE – p.php
<?php ob_start(); session_start(); echo "Hellow".$_COOKIE['LoginId']; echo "<br />"; echo "Password=".$_COOKIE['Password']; echo "<br />"; if(isset($_SESSION['LoginId'])){ unset($_SESSION['LoginId']); echo "<a href="/home.php">Logout</a>"; } else { header("location: home.php"); } ?>
Then we create a php page that get data from html form and compare it to the database the coding of page is describe below save that page with match.php.
Here we again print session value and the query string value is show on toolbar. As:-
<a href="http://localhost/xyz/p.php?id=001.?Pwd=dc5c7986daef50c1e02ab09b442ee34f"> <?php echo p.php?id=001.?Pwd=dc5c7986daef50c1e02ab09b442ee34f;?> </a>
and when we click logout hyperlink the session is expired and the page will redirect on login page