在每个页面刷新时创建新会话

时间:2013-12-28 09:44:08

标签: php session sessionid

我正在开发一个PHP应用程序,其中,我编写了一个Session类。但是,我遇到了一个奇怪的问题。每次刷新页面时都会创建一个新会话。

此外, C:\ xampp \ tmp 是可写的(我在xampp上), session_id()始终返回null

以下是我的会话类

<?php
/**
 * Class and Function List:
 * Function list:
 * - __construct()
 * - start()
 * - stop()
 * - generate_sid()
 * - set()
 * - delete()
 * - get()
 * - check()
 * - flash()
 * Classes list:
 * - Session
 */
class Session
{

public $flashElements = array();

public function __construct($autoStart = true) 
{
    $this->started = isset($_SESSION);
    e("The Session Id is " . session_id());
    if (!is_writable(session_save_path())) 
    {
        echo 'Session save path "' . session_save_path() . '" is not writable!';
    }
    e(session_save_path());
    if ($this->started && $autoStart === false) 
    {
        $this->start();
    }
    e("The Session Id is " . session_id());
}

public function start() 
{
    if (!$this->started) 
    {
        session_id($this->generate_sid());
        session_start();
        $this->started = true;
    }
}

public function stop($clearCookie = true, $clearData = true) 
{
    if ($this->started) 
    {
        if (($clearCookie) && Configure::get('session.useCookie')) 
        {
            $params = session_get_cookie_params();
            setcookie(session_name() , '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
        }

        if ($clearData) 
        {
            $_SESSION = array();
        }

        session_destroy();
        session_write_close();
        $this->started = false;
    }
}

public function generate_sid($chars = 100, $alpha = true, $numeric = true, $symbols = true, $timestamp = true) 
{
    if ($chars < 0 || !is_numeric($chars)) 
    {

        return false;
    }
    $salt = Configure::get('security.salt');

    if ($alpha) 
    {
        $salt.= 'abcdefghijklmnopqrstuvwxyz';
    }

    if ($numeric) 
    {
        $salt.= '1234567890';
    }

    if ($symbols) 
    {
        $salt.= '-_';
    }

    $sid = null;

    for ($i = 1;$i <= $chars;$i++) 
    {
        $sid.= $saltmt_rand(0, strlen($salt) - 1);
        if (mt_rand(0, 1) === 1) 
        {
            $sid
            {
                strlen($sid) - 1} = strtoupper($sid
                {
                    strlen($sid) - 1});
                }
            }

            if ($timestamp) 
            {
                $sid.= time();
            }

            return $sid;
        }

        public function set($keyword, $value) 
        {
            $_SESSION[$keyword] = $value;
        }

        public function delete($keyword) 
        {
            unset($_SESSION[$keyword]);
            $this->flashElements[$keyword] = null;
            unset($this->flashElements[$keyword]);
        }

        public function get($keyword) 
        {
            $returnVar = isset($_SESSION[$keyword]) ? $_SESSION[$keyword] : false;
            if (isset($this->flashElements[$keyword])) 
            {
                $this->delete($keyword);
            }
            return $returnVar;
        }

        public function check($keyword) 
        {
            return isset($_SESSION[$keyword]) ? true : false;
        }

        public function flash($value) 
        {
            $this->set('flash', $value);
            $this->flashElements['flash'] = $value;
        }
    }

请建议我哪里出错

2 个答案:

答案 0 :(得分:1)

我认为您没有创建会话,因为您只在

时调用start函数
$autostart === false

默认情况下为真;

答案 1 :(得分:0)

我很傻。搞定了。

更改了__construct()函数

中的以下代码
if ($this->started == false && $autoStart != false) 
{
    $this->start();
}

谢谢大家!