Function Details for getting started with PHPsession 3
Source: Internet
Author: User
Session application example the following sample is for reference only. you can customize or not customize the session, as you can (1) for user authentication & lt ;? Php & nbsp; session_start (); & nbsp; & nbsp; & nb 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 = "selectuseridfromreguserwhereuserid = $ useridandpass = $ 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
}
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 ";
}
?>
(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
?>
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