III. session application examples the following sample pieces are for reference only. you can customize or not customize the session as needed (1) for user authentication? Session_start (); $ dbhmysql_connect (localhost: 3306, xxxx, xxxx); mysql_select_db (znsoft); select database $ queryselectuseridfromreguserwhereuserid 3. session application example
The following examples are for reference only. you can customize or not customize the session.
(1) used for user authentication
Session_start ();
$ Dbh = mysql_connect ("localhost: 3306", "xxxx", "xxxx ");
Mysql_select_db ("znsoft"); // select a database
$ Query = "select userid from reguser where userid = '$ userid' and pass =' $ pass '";
// $ Userid $ pass is the user name and password passed by the login form
$ Res = mysql_query ($ query, $ dbh );
If ($ row = mysql_fetch ($ res ))
{
$ Reguser = $ row [0];
?>
Script
Alert ("OK, buddy, welcome! ");
Script
}
Else
{
$ Reguser = "";
?>
Script
Alert ("sorry, you are not a registered user! ");
Script
// Put the code on your own
}
Session_register ("reguser ");
?>
Check whether you have logged on to the other page
======================
Session_start ();
If (isset ($ reguser) & $ reguser! = "") // You have logged on
{
Echo "Welcome, buddy ";
}
Else // no logon
Echo "register now ";
?>
Exit
====================================
Session_destroy ();
// Or $ reguser = "";
?>
(2) used to pass variables
This program is used to pass variables between pages
$ Name = "m. y ";
If (! Sesion_is_registered ("name") // The session variable name is not registered.
Session_register ("name"); // register the variable name
?>
Page 2
==============================
Echo $ name;
// You don't want to use it anymore. delete it.
If (session_is_registered ("name") // whether to register, if you have already registered
Session_unregister ("name"); // delete It of course
?>
<全文完>