Session full Tutorial (iii)

Source: Internet
Author: User

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");
?>

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.