Popular Posts

Dec 29, 2010

Login Script (Rebember Me)



To make a login system with remember me option follow the steps and try to understand the code...

step1: Make a database and a table.
DROP DATABASE loginDB;
CREATE DATABASE loginDB;
USE loginDB;
CREATE TABLE tbl_Login(
id int(2) NOT NULL AUTO_INCREMENT,
email varchar(45) NOT NULL,
password varchar(45) NOT NULL,
primary key (id)
);
insert into tbl_Login (email,password) VALUES ('jubayer1',SHA('123456'));

step2: Create a file named it config.php then type the code.This file the database connection.

<?php
$connect = mysql_connect('localhost','root','1111111111') or die(mysql_error());
$selectDB = mysql_select_db('loginDB') or die(mysql_error());
?>

step3: Create a file named it login.php.
<?
require_once('config.php');
session_start();
$msg = "";
if(isset($_POST['posted'])){
$email = mysql_escape_string(trim($_POST['email']));
$password = mysql_escape_string(trim($_POST['password']));
if(!empty($email) && !empty($password)){
$getInfo = "SELECT * FROM tbl_Login WHERE email='$email' AND password=SHA('$password')";
$result = mysql_query($getInfo) or die(mysql_error());
if(mysql_num_rows($result)==1){
while($rows = mysql_fetch_array($result)){
$_SESSION['email'] = $rows['email'];
$_SESSION['password'] = $rows['password'];
if(isset($_POST['rebember'])){
setcookie('email', $rows['email'], time() + (60 * 60 * 24 * 7));    // expires in 7 days
setcookie('password', $rows['password'], time() + (60 * 60 * 24 * 7));  // expires in 7 days
}
$home = "logged_user.php";
header ('Location:'.$home);
}
}else{
 $msg = "Not found in the database...";
}
}else{
 $msg = "Empty password/email";
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<table width="50%">
<tr><td>
<fieldset><legend>Login</legend>
<? echo $msg; ?>
<form name="loginFrm" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Email</label>&nbsp;<input type="text" name="email" /><br /><br />
<label>Password</label>&nbsp;<input type="password" name="password" /><br />
<input type="hidden" name="posted" value="true" />
<label><input type="checkbox" name="rebember" value="true" /></label>&nbsp;<label>Rebember me for seven days</label><br />
<input type="reset" name="reset" value="Reset" />&nbsp;<input type="submit" name="submit" value="Login" /><br />
</form>
<label><a href="#"><label>Forgot password</label></a>
</fieldset>
</td>
</tr>
</table>
</body>
</html>






step4: Create a file named it auth_user.php

<?php
session_start();
if($_SESSION['email'] == "" && $_SESSION['password']==""){
$url = 'login.php';
header('Location:'.$url);
}
?>

step5: Create a file named it logOut.php

<?php
session_start();
session_unset();
session_destroy();
if(isset($_COOKIE['email']) && isset($_COOKIE['password'])){
   setcookie("email", "", time()-60*60*24*100, "/");
   setcookie("password", "", time()-60*60*24*100, "/");
}
if($_SESSION['email'] !="" && $_SESSION['password'] !=""){
$url = 'login.php';
header('Location:'.$url);
}else{
$url = 'login.php';
header('Location:'.$url);
}
?>

step6: Create a file named it logged_user.php

<?
session_start();
include 'auth_user.php';
$user = $_SESSION[email];
echo '<br>';
?>
&nbsp;&nbsp;&nbsp;&nbsp;<p align="right"><a href="logOut.php">logOut</a></p>
<h1>Hello [<?=$user;?>]</h1>
<hr />

No comments:

Post a Comment