Third, the session application example
The following example fragments are for reference only, you can customize or not customize the session, as you would
(1) for user authentication
?
Session_Start ();
$DBH =mysql_connect ("localhost:3306", "xxxx", "xxxx");
mysql_select_db ("Znsoft");//Select Database
$query = "selectuseridfromreguserwhereuserid= ' $userid ' andpass= ' $pass '";
$userid $pass is the username and password that the login form passes over
$res =mysql_query ($query, $DBH);
if ($row =mysql_fetch ($res))
{
$reguser = $row [0];
?>
<script>
Alert ("OK, buddy, welcome you!");
</script>
?
}
Else
{
$reguser = "";
?>
<script>
Alert ("Sorry, you are not a registered user!");
</script>
?
You put the code on yourself.
}
Session_register ("Reguser");
?>
Check to see if you're logged on in another page
================
?
Session_Start ();
if (Isset ($reguser) && $reguser!= "")//already logged in
{
echo "Welcome you, Buddy";
}
else//didn't log in!
echo "Please sign up";
?>
Exit function
===============================
?
Session_destroy ();
or $reguser= "";
?>
(2) for passing variables
This program is used to pass variables between pages
?
$name = "M.y";
if (!sesion_is_registered ("name"))//does not register session variable name
Session_register ("name");//Register variable name
?>
Second page
===================
?
Echo$name;
Don't want to use it, erase it
if (session_is_registered ("name"))//is registered if it has been registered
Session_unregister ("name");
?>