Tuesday 25 September 2012

Login and Logout Script Using Php and Mysql






login.php

<?php session_start(); ?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("test");
if(isset($_POST['submit'])&&($_POST['txtuser']!=""))
{
$sql="select * from login where username='".$_POST['txtuser']."' and password='".$_POST['txtpass']."'";
$nsql=mysql_query($sql);
$nofrow=mysql_num_rows($nsql);
if($nofrow>=1)
{
   
    $_SESSION['user']=$_POST['txtuser'];
    ?>
    <script language="javascript">
    window.location="index.php";
    </script>
    <?php

}

if($nofrow<=0)
{
    $_SESSION['user']="";
    echo "Please enter Valid Username and Password";
   

}

}

?>
<form method="post">
<table align="center" border="1">
<tr>

    <td>UserName</td>
    <td><input type="text" name="txtuser" /></td>
</tr>
<tr>
    <td>Password</td>
    <td><input type="password" name="txtpass" /></td>
</tr>
<tr>
    <td><input type="submit" name="submit" value="Submit" /></td>
    <td><input type="reset" name="reset" value="Cancle" />
</tr>

</table>
</form>
</body>
</html>










index.php

<?php session_start(); ?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<a href="logout.php">Logout</a>
<br />

<?php
if($_SESSION['user']=="")
{

    ?>
    <script language="javascript">
    window.location="login.php";
    </script>
    <?php

}
if(isset($_SESSION['user']))
{

    echo "Welcome to ".$_SESSION['user'];
}
else
{

    header("Location:login.php");
}

?>


</body>
</html>










logout.php

<?php session_start();

if(isset($_SESSION['user']))
{
    unset($_SESSION['user']);
    $_SESSION['user']="";
    ?>
    <script language="javascript">
    window.location="login.php";
    </script>
   
    <?php
   

}


?>










Share This
Previous Post
Next Post

0 Comments: