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= "$" 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 ";
}
}
Else
{
echo ";
}
}
Else
{
echo ";
}
? >
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 "";
}
? >
Oh, today said here, tomorrow say how to get a pagination.