Session full tutorial (III)

Source: Internet
Author: User
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
?>


<全文完>

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.