php实现最基础的session

  • session存在于服务端,鉴权依赖于cookie
<?php

new MySession();

class MySession{
    public $path = 'D:\testproject\\';
    public $sessionhash = [];
    public function __construct()
    {   
        if(!empty($_COOKIE['PHPSESSID']) && $this->checkSession($_COOKIE['PHPSESSID'])){
            echo  '<br />session合理';
        }else{
            $this->productSession(uniqid());
            echo '<br />session已创建';
        }
    }
    // 生成session
    function productSession($sessionname){
        $sessionFile =  $this->path . $sessionname;
        setcookie("PHPSESSID", $sessionname);
        file_put_contents($sessionFile, time());
    }
    // 检测ssion
    function checkSession($sessionname){
        $file = $this->path . $sessionname;
        
        if(is_file($file)){
            $content = file_get_contents($file);
            echo '<br />session创建时间戳:'. $content;
            return true;
        }
        return false;
    }
    
}
posted @ 2021-01-07 17:03  cl94  阅读(95)  评论(0编辑  收藏  举报