ychw0776
ychw0776
采纳率0%
2017-08-22 03:46 阅读 2.5k

PHP Session页面跳转值丢失

5

Session入库 页面跳转值丢失问题

我想在用户登录之后用session存储用户信息然后放到数据库中,
登录成功3秒后会跳转到另一个页面,但是SESSSION存储的值仅仅在数据库中
存放了这3秒,页面跳转过去之后Session的值就没了。
跳转过程中是这样的
图片说明
跳转过去就没了
图片说明

 <?php
class SessionLib{

    private $db;

    public function __construct(){
        ini_set('session.save_handler','user');

        session_set_save_handler(
            array($this,'open'),
            array($this,'close'),
            array($this,'read'),
            array($this,'write'),
            array($this,'destroy'),
            array($this,'gc')
        );

    }

    public function open(){
        $this->db = MysqlDB::getInstance($GLOBALS['config']['database']);
        echo '开启会话<br>';
    }

    public function close(){
        echo '关闭会话<br>';

    }

    public function read($session_id){
        $sql = "select session_value from session where session_id = $session_id";
        echo '读取会话<br>';
        return $this->db->fetchColum($sql);
    }

    public function write($session_id,$session_value){
        $time = time();
        $sql="insert into session values ('$session_id','$session_value',$time)
        on duplicate key update session_value='$session_value'";
        echo '写入会话<br>';
        return $this->db->query($sql);
    }

    public function destroy($session_id){
        $sql = "delete from session where session_id = $session_id";
        echo '删除会话<br>';
        return $this->db->query($sql);

    }

    public function gc($maxlifetime){
        $time =  time()- $maxlifetime ;
        $sql = "delete from session where session_time < $time";
        return $this->db->query($sql);

    }


}

 <?php
class Controller{

    public function __construct(){
        $this->initSession();
    }

    private function initSession(){
        new SessionLib();
        session_start();
    }

    public function success($url,$msg,$time=1){
        $this->jump($url,$msg,$time,true);
    }

    public function error($url,$msg,$time=2){
        $this->jump($url,$msg,$time,false);
    }


    private function jump($url,$msg,$time=3,$flag){
        if($msg == ''){
           header("location:{$url}");
        }else{
            if($flag){
                $img = '<img src="Public/Images/success.png">';
            }else{
                $img = '<img src="Public/Images/error.png">';
            }
        }
        echo <<<jump
        <!DOCTYPE html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta http-equiv="refresh" content="{$time};URL={$url}" />
            <style>
            div{
                margin-top: 100px;
                margin-left: 600px;
            }
            </style>
            <title>jump</title>
            </head>
            <body>
            <div>  {$img}    <p>{$msg}</p></div>
            </body>
            </html>
jump;
    }




}

 <?php
class UserLoginController extends Controller{

    public function LoginAction(){
        $model = new UserModel();
        if(isset($_POST['submit'])){
            $data['username'] = $_POST['username'];
            $data['password'] = $_POST['password'];
            $_SESSION['user'] = $data['username'].md5($data['password']);
            if($model->find($data)){
                $this->success('index.php?p=Home&&c=MainPage&&a=MainPage','登录成功');
                exit;
            }else{
                echo '登录失败';
            }
        }
        require __VIEW__.'UserLogin.html';
    }

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • caozhy 从今以后生命中的每一秒都属于我爱的人 2017-08-22 16:28
    点赞 1 评论 复制链接分享
  • qq_33634563 Sculptor_real 2018-12-18 06:00

    老铁目测你是看某智的视频,这个问题在你写的read方法里面。

    public function read($session_id){
            $sql = "select session_value from session where session_id = $session_id";
            echo '读取会话<br>';
            return $this->db->fetchColum($sql);
        }
    

    这里的$sql应该写成 “select * from session_web where sess_id='$sess_id'”; 因为这里最后调用的是fetchColum($sql),具体你看看你fetchColum($sql)里的代码就明白了。 另外不得不说这个视频质量巨差,讲师讲课乱七八糟,顺序很混乱,普通话还带巨大口音。大兄弟你能坚持看到这儿来真不容易。。。lol

    点赞 评论 复制链接分享

相关推荐