Learning purpose: Learn how to use SESSION
SESSION has many functions, the most commonly used one is the transfer of variables between pages within the site. At the beginning of the page, we need session_start(); to turn on SESSION;
Then you can use the SESSION variable. For example, to assign a value: $_SESSION['item']="item1"; to get the value, $item1=$_SESSION ['item'];, very simple. Here we may use some functions, for example, to determine whether a SESSION variable is empty, you can write: empty($_SESSION['inum']) returns true or false.
Let’s take a look at a login procedure to determine whether the username and password are correct.
The login form is like this: login.php
This is how the file is processed
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" );
?>
Since $_SESSION['adminname']=$username; we can write the file to verify the login statement like this: checkadmin.asp
session_start ();
if($_SESSION['adminname']=='')
{
echo "<script>alert('Please Login First');location.href='login.php ';</script>";
}
?>
Haha, that’s it for today, let’s talk about how to make a paging tomorrow.