PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

PHP Login Script Remember Me Feature

October9

PHP login logout system with remember me feature.

To have remember me feature installed in the login script you have to use cookies. Cookies are temporary files on the personal computer where an application can store information to be used later.

First you have to create a mysql DB to store the username and password

CREATE DATABASE `login` ;

CREATE TABLE IF NOT EXISTS `adminuser` (
`sno` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`username`)
);

INSERT INTO `adminuser` (`sno`, `username`, `password`) VALUES
('1', 'admin', '5f4dcc3b5aa765d61d8327deb882cf99');

We have to create five files

  • config.php
  • index.php
  • login.php
  • checklogin.php
  • logout.php
  • homepage.php

config.php

<?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('egames', $conn) or die(mysql_error());
?>

index.php

<?php session_start(); ?>
<html>
<head>
<title>My Website</title>
</head>
<body>
<?php
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
$_SESSION['username'] = $_COOKIE['cookname'];
$_SESSION['password'] = $_COOKIE['cookpass'];

/* Validating username and password from the database */
include("config.php");
$username=$_SESSION['username'];
$password=$_SESSION['password'];
$ress=mysql_query("select * from adminuser where username='$username'") or die(mysql_error());
$rows=mysql_fetch_array($ress);
if(($rows["username"]==$username)&&($rows["password"]==$password)) include("homepage.php");
else { echo "wrong username or password. <a href='login.php'>click here</a> to login again"; }
/* Validating username and password from the database */
} else { include("login.php"); } ?>
</body>
</html>

login.php

<form method="post" action="checklogin.php">
Please enter your Username and Password:<br>
Username: <input type="text" name="username" size="30"><br>
Password: <input type="password" name="password" size="30"><br>
<input type="checkbox" name="remember" /> Remember Me<br /><br />
<input type="submit" value="Submit" name="Submit">
</form>

checklogin.php

<?php
session_start();
/* Validating username and password from the database */
include("config.php");
$username=$_POST["username"];
$password=md5($_POST["password"]);
$ress=mysql_query("select * from adminuser where username='$username'") or die(mysql_error());
$rows=mysql_fetch_array($ress);

if(($rows["username"]==$username)&&($rows["password"]==$password))
{
$_SESSION['username']=$username;
$_SESSION['password']=$password;
if(isset($_POST['remember'])){
setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
}

header ("Location: homepage.php");

}
else echo "wrong username or password";
?>

logout.php

<?php
session_start();
session_destroy();
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
setcookie("cookname", "", time()-60*60*24*100, "/");
setcookie("cookpass", "", time()-60*60*24*100, "/");
}

header ("Location: index.php");
?>

homepage .php

<?php
session_start();
if(!(isset($_SESSION['username']))){ ?> <script language="JavaScript">window.open("index.php", "_self")</script><? }?>
<html>
<head><title>My Homepage</title></head>
<body>
<A href="homepage.php">HOME</A><br /><br />
WELCOME TO MY SECURE WEBSITE<br>
<a href="logout.php">Logout</a></body>
</html>

Code Explanation
config.php connecting to the database
index.php
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
$_SESSION['username'] = $_COOKIE['cookname'];
$_SESSION['password'] = $_COOKIE['cookpass'];
checking if the cookies storing username and password has been set
if yes then set sessions with the information of username password from cookie
/* Validating username and password from the database */
include("config.php");
$username=$_SESSION['username'];
$password=$_SESSION['password'];
$ress=mysql_query("select * from adminuser where username='$username'");
$rows=mysql_fetch_array($ress);
if(($rows["username"]==$username)&&($rows["password"]==$password)) include("homepage.php");
else { echo "wrong username or password. <a href='login.php'>click here</a> to login again"; }
/* Validating username and password from the database */
} else { include("login.php"); } ?>
comparing the cookie information with database information
If information matches then open homepage.php else open login.php
login.php simple html form using method ‘post’ and action ‘checklogin’ with remember me textbox
checklogin
<?php
session_start();
/* Validating username and password from the database */
include("config.php");
$username=$_POST["username"];
$password=md5($_POST["password"]);
$ress=mysql_query("select * from adminuser where username='$username'")
$rows=mysql_fetch_array($ress);

if(($rows["username"]==$username)&&($rows["password"]==$password))
{
$_SESSION['username']=$username;
$_SESSION['password']=$password;
if(isset($_POST['remember'])){
setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
}

header ("Location: homepage.php");

}
else echo "wrong username or password";
?>

Getting username and password from login.php page

Compare the username and password with data in the database

If information matches set sessions and if “remember me” textbox was set form login.php then set cookies to store username and password and open homepage.php else give message of wrong username and password

logout.php logs out all sessions and cookies
homepage.php
if(!(isset($_SESSION['susername']))) check if the username isset
<script language="JavaScript">window.open("index.php", "_self")</script> if not go back to the index.php page

Check http://www.evolt.org/article/comment/17/60265/index.html for a different coding style of the same script

posted under Development Guide

Email will not be published

Website example

Your Comment:

 

3,063 spam comments
blocked by
Akismet