Wednesday, February 12, 2014

Login and Session Control in PHP

Let's see how to manage the Session in Web pages before that that we need to know what is the the Session and why it's need to web pages .Session mean we temporarily save or pass data to verify or to do some other actions  .Session will help to authenticate whether the request to access some other pages come from from authenticated user or else coming from other illegal access



<title>Login Page</title>

<Head><center>Login</center>

<script language="javascript">

    function validate(form)

    {

        var uname=form.uname.value;

        var pword=form.pword.value;

        if(uname==""||pword="")

        {

            alert("please fill all ");

            return false;

        }

        else

        {

            return true;

        }

    }

</script>

</head>

<div>

<body>

<form name="login" Method="POST" OnSubmit="return validate(login)" 
      action="check.php">

<center><table border="1">

<tr><td >Username</td><td><input type="text" name="uname"></td></tr>

<tr><td>PassWord</td><td><input type="text" name="Pword"></td></tr>

<tr><td></td> <td align="right"><input type="submit" value="submit"></td></tr>

</table></center>

</form>

</div>

</body>



/////////////////////////////////////////////check.php



    $uname=$_POST['uname'];

    $pword=$_POST['Pword'];



    $db=mysql_connect('localhost','root','') 
          or die('database connection is error');
    mysql_select_db('sms',$db);
    $sql="select * from usercredentials where Username='$uname'";
    $result1=mysql_query($sql);
    $row=mysql_fetch_array($result1);



    if($pword==$row['Password']&&$row['Privilege']=='admin')

        {
            session_start();
            $_SESSION['uname']=$uname;
            header("location:admin.php");
        }
    else if($pword==$row['Password']&&$row['Privilege']=="student")

        {
            session_start();
            $_SESSION['uname']=$uname;
            header("location:student.php");
        }

    else
        {
            header("location:index.html");
        }
    ?> 
 
 
////////////////////////////////////////////////Logout.php
 
 <?php

    session_start();

    if(isset($_SESSION['uname']))

            {
                session_destroy();
                header("location:index.html");
            }

    else
            {
            header("location:index.html");
            }

?>

No comments:

Post a Comment