SESSION入库的实现

来源:互联网 发布:国内旅游租房软件 编辑:程序博客网 时间:2024/04/30 09:42

session入库可以解决哪些问题?

1.      可以解决跨域操作

2.      可以实现单点登陆

3.      可以统计在线人数

4.      可以实现同一时只允许一个用户在线

session_set_save_handler的回掉函数描述

实现session入库

       第一步:在PHP.ini配置文件中设置session.save_headler=user(默认是file)

                     或者使用ini_set设置ini_set(‘session.save_handler’,’user’);

       第二步:创建一个存放session的数据表

                            session_id用于存放session_id的,字段类型为字符型,长度为32

              session_value用于存放session的内容,字段类型为text

session_life   用于存放session的生存期

    第三步:session_set_save_handler ( callback $open , callback $close ,callback $read , callback $write , callback $destroy , callback $gc )

    第四步:代码实现

<?phpheader('content-type:text/html;charset=utf-8');//将session存储方式设置为存入数据库的方式//session.save_handler = filesini_set("session.save_handler", "user");session_set_save_handler("open", "close", "read", "write", "destroy", "gc");//打开并连接数据库function open(){    //使用pdo    $pdo = new PDO('mysql:host=127.0.0.1;dbname=yii','root','root');    $pdo->exec('set names utf8');}//关闭连接function close(){    //使用pdo    $pdo = new PDO('mysql:host=127.0.0.1;dbname=yii','root','root');    $pdo->exec('set names utf8');    $pdo = null;}//从表中中读信息function read($session_id){    //使用pdo    $pdo = new PDO('mysql:host=127.0.0.1;dbname=yii','root','root');    $pdo->exec('set names utf8');    $read = $pdo->query("select * from session where session_id='$session_id'")->fetch(PDO::FETCH_ASSOC);    return $read["session_info"];}//将session存入数据库function write ($session_id,$session_info){    // echo $session_id. "        " . $session_info;    //使用pdo    $pdo = new PDO('mysql:host=127.0.0.1;dbname=yii','root','root');    $pdo->exec('set names utf8');    $read = $pdo->query("select * from session where session_id='$session_id'")->fetch(PDO::FETCH_ASSOC);    if(empty($read))    {        $time = time();        $pdo->exec("insert into session (session_id, session_info, session_life) values ('$session_id', '$session_info', '$time')");    }    else    {        $sql = "update session set session_info='$session_info' where session_id='$session_id'";        $pdo->exec($sql);    }}//销毁指定sessionfunction destroy($session_id){    //使用pdo    $pdo = new PDO('mysql:host=127.0.0.1;dbname=yii','root','root');    $pdo->exec('set names utf8');    $pdo->exec("delete from session where session_id='$session_id'");}//删除所有过期的sessionfunction gc(){}session_start();//判断是否有sessionif(isset($_SESSION['name'])){    $status = 1;}else{    $status = 0;}//获取需要接收的值$user_name = isset($_POST['user_name'])?$_POST['user_name']:null;$user_pwd = isset($_POST['user_pwd'])?$_POST['user_pwd']:null;//使用pdo$pdo = new PDO('mysql:host=127.0.0.1;dbname=yii','root','root');$pdo->exec('set names utf8');$sql = "select * from user where username = '$user_name' and password = '$user_pwd' ";$re = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);if($re){    $status = 1;    $_SESSION['name'] = $user_name;}?><!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><body><?php if($status == 0){?>    <h3>服务器3</h3>    <form action="a.php" method="post">        <table>            <tr>                <td>姓名:</td>                <td><input type="text" name="user_name"></td>            </tr>            <tr>                <td>密码:</td>                <td><input type="password" name="user_pwd"></td>            </tr>            <tr>                <td></td>                <td><input type="submit" value="登录"></td>            </tr>        </table>    </form><?php } else{?>    <h2>欢迎<?=$_SESSION['name']?>登录</h2>    <h3>服务器3</h3><?php }?></body></html>


2 0
原创粉丝点击