Objective: To learn how to use a SESSION
SESSION has many functions, most of which are the variable transfer between pages on the site. At the beginning of the page, we want session_start (); To enable SESSION;
Then you can use the SESSION variable. For example, the value to be assigned is $ _ SESSION ['item'] = "item1 "; the value to be obtained is $ item1 = $ _ SESSION ['item'];, which is simple. Here we may use some functions. For example, to determine whether a SESSION variable is null, we can write: empty ($ _ SESSION ['inum']) and return true or false.
Next, let's take a look at the above description to check whether the user name and password are correct.
The login form is as follows: 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>
This is the case when processing files.
<?
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 a file to verify whether the statement is logged on: checkadmin. asp.
<?
Session_start ();
If ($ _ SESSION ['adminname'] = '')
{
Echo "<script> alert ('Please Login First '); location. href = 'login. php'; </script> ";
}
?>
Let's talk about how to create a page tomorrow.