Learning Purpose: Learn the use of Session
The role of a lot of sessions, the most use is the site within the page variable transfer. At the beginning of the page we want to session_start ();
Then you can use the session variable, for example, to assign the value is: $_session[' item ']= ' item1 '; to get the value is $item1=$_session[' item '; Here we may use some functions, such as to determine whether a session variable is empty, so to write: empty ($_session[' Inum ') returns TRUE or false.
Here is a comprehensive look at a login program to determine whether the user name password is correct.
The landing form is this: login.php
<table width= "100%" height= "100%" border= "0" align= "center" cellpadding= "0" cellspacing= "0" >
<tr>
<form action= "checklogin.php" method= "POST" ><td align= "center" valign= "middle" ><table width= "400" Border= "0" cellpadding= "5" cellspacing= "1" class= "TABLEBG" >
<tr class= "tdbg" >
<TD colspan= "2" ><div align= "center" >administrators login</div></td>
</tr>
<tr class= "tdbg" >
<td><div align= "center" >Username</div></td>
<td><div align= "center" >
<input name= "username" type= "text" id= "username" >
</div></td>
</tr>
<tr class= "tdbg" >
<td><div align= "center" >Password</div></td>
<td><div align= "center" >
<input name= "password" type= "password" id= "password" >
</div></td>
</tr>
<tr class= "tdbg" >
<TD colspan= "2" ><div align= "center" >
<input type= "Submit" name= "submit" value= "Submit" >
<input type= "reset" name= "Submit2" value= "clear" >
</div></td>
</tr>
</table></td></form>
</tr>
</table>
Working with files is like this
?
Require_once (' conn.php ');
Session_Start ();
$username =$_post[' username '];
$password =$_post[' password '];
$exec = "SELECT * from admin where username= '". $username. "'";
if ($result =mysql_query ($exec))
{
if ($rs =mysql_fetch_object ($result))
{
if ($rs->password== $password)
{
$_session[' AdminName ']= $username;
Header ("location:index.php");
}
Else
{
echo "<script>alert (' Password Check error! '); location.href= ' login.php ';</script> ';
}
}
Else
{
echo "<script>alert (' Username Check error! '); location.href= ' login.php ';</script> ';
}
}
Else
{
echo "<script>alert (' Database Connection error! '); location.href= ' login.php ';</script> ';
}
?>
Conn.php is like this:
?
$conn =mysql_connect ("127.0.0.1", "" "," ");
mysql_select_db ("Shop");
?>
Because $_session[' adminname ']= $username; we can write this to verify that the file is logged: checkadmin.asp
?
Session_Start ();
if ($_session[' adminname ']== ')
{
echo "<script>alert (' please Login a '); location.href= ' login.php ';</script>";
}
?>
Oh, today said here, tomorrow say how to get a pagination.