Home > Backend Development > PHP Tutorial > Full session tutorial (3)_PHP tutorial

Full session tutorial (3)_PHP tutorial

WBOY
Release: 2016-07-13 17:11:33
Original
817 people have browsed it

3. Session application examples

The following example snippets are for reference only. You can customize the session or not, as you like
(1) Used 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 passed by the login form
$res=MySQL_query($query,$dbh);
if($row=MySQL_fetch($res))
{
$reguser=$row[0];
?>
<script> <br> alert("ok, buddy, welcome!"); <br> </script>
}
else
{
$reguser="";
?>
<script> <br> alert("sorry, you are not a registered user!"); <br> </script>

//Put the code yourself
}
session_register("reguser");
?>
Check whether you are logged in on another page
================
session_start();

if(isset($reguser)&&$reguser!="")//Already logged in
{
echo "Welcome, buddy";
}
else//Not logged in
echo "Please register";
?>
Exit function
===============================
session_destroy();
//or $reguser="";
?>

(2) is used to pass variables

This program is used to transfer variables between pages
$name="m.y";
if(!session_is_registered("name"))//The session variable name is not registered
session_register("name");//Register variable name
?>
Second page
===================
echo$name;

//If you don’t want to use it anymore, delete it
if(session_is_registered("name"))//Whether to register, if already registered
session_unregister("name");//Delete it of course
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629566.htmlTechArticle3. Session application examples The following examples are for reference only. You can customize the session or not, it’s up to you. Is (1) used for user authentication? session_start(); $dbh=MySQL_connect(localh...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template