Home > php教程 > php手册 > session全教程(三)

session全教程(三)

WBOY
Release: 2016-06-13 10:13:17
Original
1119 people have browsed it

三、session应用举例

以下这些例子片断仅供参考,你可以定制也可以不定制session,随你便
(1)用于用户认证

session_start();
$dbh=MySQL_connect("localhost:3306","xxxx","xxxx");
MySQL_select_db("znsoft");//选择数据库
$query="selectuseridfromreguserwhereuserid='$userid'andpass='$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");//当然删掉啦
?>

 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template