Home >Backend Development >PHP Tutorial >Detailed explanation of the function of getting started with PHP session 3_PHP tutorial
Session application examples
The following example fragments 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=$useridandpass=$ pass";
//$userid$pass is the username and password passed by the login form
$res=mysql_query($query,$dbh);
if($row=mysql_fetch($res)) < /script>
} else {
$reguser="";
?>
<script><br> alert("Sorry, you are not a registered user !");<br> </script>
}
session_register("reguser");
?>
Check whether you have logged in on another page
================
session_start();
if(isset ($ reguser) && $ reguser! = "") // Log in
{
echo "Welcome to you, buddy";
} else {// ";
}
?>
Exit function
============================ ====
session_destroy();
//or $reguser="";
?>
(2) is used Pass variables
This program is used to pass variables between pages
Variable name
session_register("name");//Register variable name
?>
Second page
=============== ====
echo $name;
session_unregister("name");//Delete it of course
?>
http://www.bkjia.com/PHPjc/486243.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/486243.html
TechArticle