(1)first write following code for Login form "loginform.html" file
<form action="login.php" method="post">
<table border=0>
<tr><td>User name:</td>
<td>
<input name="username" type="TEXT"></td> </tr>
<tr><td>Password</td><td>
<input name="Password" type="PASSWORD"></td>
</tr>
<tr>
<td></td><td><input value="submit" name="SubmitUser" type="submit"></td>
</form>
</tr>
</table>
(2) Then write code for "login.php" file
<?php
$username=$_POST['username'];
$password=$_POST['password'];
$con=mysql_connect("hostname","username","password") or die(mysql_error());
mysql_select_db("databasename",$con);
$sql="select username,password from login where username='$username' AND password='$password'";
$result=mysql_query($sql) or die(mysql_error());
$num=mysql_num_rows($result) or die(mysql_error());
if($num>0)
{
session_start();
$_SESSION['username']=$username;
header("Location:dashboard.php");
}
else if($num==0)
{
header("Location:error.php");
}
?>
(3)Session management code:
<?php
session_start();
if(!isset($_SESSION['username']))
{
header("Location:index.php");
}
?>
NOte:
write this code on every your page starting or simply save it as
session.php & on every page use it as
<?php include("session.php"); ?>
(4)Logout code : save file name as "logout.php"
see below code:
<?php
session_start();
session_destroy();
header("Location:loginform.html");
?>
<form action="login.php" method="post">
<table border=0>
<tr><td>User name:</td>
<td>
<input name="username" type="TEXT"></td> </tr>
<tr><td>Password</td><td>
<input name="Password" type="PASSWORD"></td>
</tr>
<tr>
<td></td><td><input value="submit" name="SubmitUser" type="submit"></td>
</form>
</tr>
</table>
(2) Then write code for "login.php" file
<?php
$username=$_POST['username'];
$password=$_POST['password'];
$con=mysql_connect("hostname","username","password") or die(mysql_error());
mysql_select_db("databasename",$con);
$sql="select username,password from login where username='$username' AND password='$password'";
$result=mysql_query($sql) or die(mysql_error());
$num=mysql_num_rows($result) or die(mysql_error());
if($num>0)
{
session_start();
$_SESSION['username']=$username;
header("Location:dashboard.php");
}
else if($num==0)
{
header("Location:error.php");
}
?>
(3)Session management code:
<?php
session_start();
if(!isset($_SESSION['username']))
{
header("Location:index.php");
}
?>
NOte:
write this code on every your page starting or simply save it as
session.php & on every page use it as
<?php include("session.php"); ?>
(4)Logout code : save file name as "logout.php"
see below code:
<?php
session_start();
session_destroy();
header("Location:loginform.html");
?>
No comments:
Post a Comment