The seventh day of learning php in ten days

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.