Home > Backend Development > PHP Tutorial > session全教程(三)_PHP

session全教程(三)_PHP

WBOY
Release: 2016-06-01 12:43:09
Original
953 people have browsed it

三、session应用举例 

以下这些例子片断仅供参考,你可以定制也可以不定制session,随你便 
(1)用于用户认证 
 
session_start(); 
$dbh = mysql_connect("localhost:3306","xxxx","xxxx"); 
mysql_select_db("znsoft");//选择数据库 
$query="select userid from reguser where userid='$userid' and pass='$pass' "; 
//$userid $pass 是登录form传递过来的用户名和密码 
$res=mysql_query($query,$dbh); 
if($row=mysql_fetch($res)) 

$reguser=$row[0]; 
?> 
<script> <br />alert("ok,哥们,欢迎你!"); <br /></script> 
 

else 

$reguser=""; 
?> 
<script> <br />alert("sorry,你不是注册用户!"); <br /></script> 
 

//你自己放上代码吧 

session_register("reguser"); 
?> 
另一页面中检查是否已经登录 
================ 
 
session_start(); 

if(isset($reguser)&&$reguser!="")//已经登录 

echo "欢迎你,哥们"; 

else//没有登录呀 
echo "请注册吧"; 
?> 
退出功能 
=============================== 
 
session_destroy(); 
//或 $reguser=""; 
?> 

(2)用于传递变量 

本程序用于在页面之间传递变量 
 
$name="m.y"; 
if(!sesion_is_registered("name"))//没有注册session变量 name 
session_register("name");//注册 变量 name 
?> 
第二页 
=================== 
 
echo $name; 

//不想用了,删掉吧 
if(session_is_registered("name"))//是否注册,如果已经注册 
session_unregister("name");//当然删掉啦 
?>



Related labels:
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