10 days Learn PHP Seventh Day _php Foundation

Source: Internet
Author: User

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" >
&LT;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" >
&LT;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.

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.