session in login

// login page

 

<form action=”session.php”  method=”post”>

Username<input type=”text” name=”t1″>

Password <input type=”password” name=”t2″>

<input  type=”submit” value=”login”>

</form>

 

//session.php

<?php

session_start(); //for start session

if(isset($_POST[t1]))

{

$con=mysql_connect(“”,””,””);  //enter your hostname,username,password

mysql_select_db(“databasename”,$con);

$query=”select * from tablename where username='”.$_POST[‘t1’].”‘”;

$result=mysql_query($query);

$data=mysql_fetch_array($result);

if(mysql_num_rows($result) !=0)

{

if($_POST[‘t2’]==$data[‘password’])

{

$_SESSION[‘username’]=$_POST[‘t1’];        //session variable

$_SESSION[‘password’]=$_POST[‘t2’];

header(“location:welcome.php”);     //welcome page

}else

{

echo “Wrong password”;

}

 

}else

{

echo “user not found”;

}

}else

{

echo “enter username”;

}

?>

//welcome page……………

<?php

session_start();

echo $_SESSION[‘username’];

echo “<a href=logout.php>Logout</a>”;

?>

 

//logout………….

<?php

session_unset();

session_destroy();

header(“location:login.php”);

?>

Advertisements

2 Comments Add yours

  1. yasmine says:

    Sir
    My problem is that your session.php page is not working for me. Everytime i enter correct Username and password, it brings me back to my form instead of going to further desired file. I have checked all the parametres and cannot find any error.
    Please help.
    Thanks

    1. Send Your Code I check

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s