Thursday, 23 May 2013

login and logout script using php

Login and logout script using php

Create database 'user_login'



CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL DEFAULT '0',
  `email` varchar(30) NOT NULL,
  `pwd` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
)



Create page config.php

<?php
$sql=mysql_connect("localhost","root","") or die("connection error");
mysql_select_db("user_login",$sql) or die("database not selected");
?>



Create page login.php


<?php
session_start();
include("config.php");
$id=$_REQUEST['id'];
$pwd=$_REQUEST['pwd'];
if($_REQUEST['login'])
{
   if($id=="" or $pwd=="")
   {
   $res="plz fill id and password";
   }
   else
   {
      $sql=mysql_query("select * from user where email='$id'");
 $data=mysql_fetch_array($sql);
      if($data['email']!=$id)
 {
 $res="please fill valid email addres";
 }
 else
 {
if($data['pwd']!=$pwd)
{
$res="plz fill valid password";
}
else
{
$_SESSION['sid']=$id;
header("location:account.php");
}
 }
   }
}
?>
<form action="" method="post">

<fieldset style="width:300px;">
<legend>Login Form</legend>
<p><?php echo $res;?></p>
<p>User Id : <input type="text" name="id"></p>
<p>Password : <input type="password" name="pwd"></p>
<p><input type="submit" name="login" value="Login"></p>
</fieldset>
</form>




Create account.php



<?php
session_start();
$log=$_REQUEST['log'];
if($log=="logout")
{
unset($_SESSION['sid']);
header("location:login.php");
}
$id=$_SESSION['sid'];
if($id=="")
{
header("location:login.php");
}



?>
<body background="theme/<?php echo $theme;?>">
<table width="100%" border="1" height="100%">
  <tr>
    <td width="16%" height="93">Welcome : <?php echo $id;?></td>
    <td width="84%">
    <p align="right"><a href="account.php?log=logout">LOGOUT</a></p></td>
  </tr>
  <tr>
    <td>
</td>
    <td align="center">&nbsp;


</td>
  </tr>
</table>



No comments:

Post a Comment