一会员系统,有一共享账号,希望实现某用户登录后,其他的用户无法登录。退出时候,必须点击退出,解锁。目前是用某字段来实现。登录为1,退出为0.但是在退出机制中,有时候会员被意外停电,无法实现正常注销。。 或者忘记注销等情况, 请问如何解决类似问题。

解决方案 »

  1.   


    官方网站给出的例子,红色的需要你追加自己的代码。<?php
    class FileSessionHandler
    {
        private $savePath;    function open($savePath, $sessionName)
        {
            $this->savePath = $savePath;
            if (!is_dir($this->savePath)) {
                mkdir($this->savePath, 0777);
            }
            return true;
        }    function close()
        {
            return true;
        }    function read($id)
        {
            return (string)@file_get_contents("$this->savePath/sess_$id");
        }    function write($id, $data)
        {
            return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true;
        }    function destroy($id)
        {
            $file = "$this->savePath/sess_$id";
            if (file_exists($file)) {
    $data = unserialize($this->read($id));
    mysql_query("UPDATE user SET status = 1 WHERE user_id = ".$data['user_id']);

                unlink($file);
            }        return true;
        }    function gc($maxlifetime)
        {
            foreach (glob("$this->savePath/sess_*") as $file) {
                if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
    $data = unserialize($this->read(str_replace("sess_", $file)));
    mysql_query("UPDATE user SET status = 1 WHERE user_id = ".$data['user_id']);

                    unlink($file);
                }
            }
            return true;
        }
    }$handler = new FileSessionHandler();
    session_set_save_handler(
        array($handler, 'open'),
        array($handler, 'close'),
        array($handler, 'read'),
        array($handler, 'write'),
        array($handler, 'destroy'),
        array($handler, 'gc')
        );// the following prevents unexpected effects when using objects as save handlers
    register_shutdown_function('session_write_close');session_start();
    ?>
      

  2.   

    退出为0的话,上面的例子应该是status = 0。另外如果退出处理里已经修改status=0的话,destroy函数可以不用改。gc处理有超时时间,非正常退出的场合,默认要等1440秒,这个时间可以设置ini_set('session.gc_maxlifetime', 600)。