没有设置PHP Session自定义类会话?

时间:2015-05-13 23:00:52

标签: php session

我正在编写一个自定义类,以便为自己编写更简单的脚本。我生成了终身会话和正常会话(正常寿命)。所以这是我创建和获取

的脚本部分

创建

    public static function create($session_name,$value="") {
        //check if the $_SESSION was started manually without using our functions...
        if(!isset($_SESSION)) {
            //init this- it creates session_start session_id 
            self::init(self::$settings["default_name"]);
        }

        //create array for serialization
        $new_session = array();

        //if our session is an array means we want this session to have some unique properties compared to others
        if( is_array($session_name)) {
            //store the session value in the array
            $new_session["value"] = $session_name["value"];
            //total time is null, this indicates if we want a life expectancy of total 10 hours total or total from current time.
            $total = null;
            if(isset($session_name["lifeclock"])) { //lifeclock should be a hh:mm:ss format to be exploded
                $clock = explode(":",$session_name["lifeclock"]); //we've exploded it now assign it
                $hours = $clock[0]; //hours
                $minutes = $clock[1]; //minutes
                $seconds = $clock[2]; //seconds
                $session_add = 0; //variable to be added to total or not technically
                if(isset($session_name["concurrent"])) {
                    $session_add = time(); //if concurrent time is true assign time to the session_add
                }
                $total = ( $session_add ) + ((int)$hours * 60 * 60) + ((int)$minutes * 60) + (int)$seconds; //broken down math to make all seconds
            }
            if(!isset($total)) { //this is why total is null
                $total = self::$settings["lifetime"]; //if null lifetime we'll use the default lifetime
            }
            session_set_cookie_params( $total, //assing all data to the session_set_cookie_params
                                       isset($session_name["path"]) ? $session_name["path"] : self::$settings["path"],
                                       isset($session_name["domain"]) ? $session_name["domain"] : self::$settings["domain"],
                                       isset($session_name["secure"]) ? $session_name["secure"] : self::$settings["secure"], 
                                       isset($session_name["httponly"]) ? $session_name["httponly"] : self::$settings["httponly"]
                                    ); 
            $new_session["life"] = $total; //we'll also add the time and when it was born
            $new_session["born"] = time(); // so the user can use this later in the programming code

            $_SESSION[$session_name["name"]] = serialize($new_session); //serialize the array
        } elseif(is_string($session_name)) {
            $new_session["value"] = $value; //assign value value
            $new_session["born"] = time(); //assign born time
            $_SESSION[$session_name] = serialize($new_session); //serialize the array
        }
        session_write_close(); //close the lock
    }

获取

    public static function get($session_name,$data = false) {
        //test if session has been opened via manual or programatically 
        if(!isset($_SESSION)) {
            self::init(self::$settings["default_name"]);
        }

        //if data argument is true meaning we don't want all the extra information we'll just return value!
        if($data === false) {
            if(isset($_SESSION[$session_name])) {
                $sess = unserialize($_SESSION[$session_name]);
                if(isset($sess["value"])){
                    return $sess["value"];
                } else return false;
            } else return false;
        } elseif($data === true) {
            return unserialize($_SESSION[$session_name]);
        }

    }

现在这是我的文件,用于完全测试。

<?php
    set_include_path(dirname($_SERVER["DOCUMENT_ROOT"]));
    require "admininit__autoload.php";

    Session::configure(array(
        "default_name"=>"boogie",
        "lifetime"=> 3600,
    ));

    Session::create( array(
        "name"=>"my_session",
        "value"=>"boogie all night long",
        "lifeclock"=>"00:05:00"
    ));

    $session_value = Session::get("my_session");

    var_dump($session_value);
    echo "<pre>";
    print_r($_SESSION);
    echo "</pre>";
?>

这就是我对var_dumpprint_r

的回应
bool(false)
Array
(
)

所以这告诉我,$session_value从get函数返回false,这意味着darn会话因某些奇怪的原因而没有保存。以下是我在google开发人员资源面板中看到的内容

sessions in google resource panel

所以我告诉我某个会话正在某个地方创建。我还继续检查我的/tmp文件夹,看不到以sess_开头的文件,这是会话的常用文件。关于我的问题所在的任何暗示,或者可能会弄清楚这里到底出了什么问题?

更新

虽然代码的创建没有注释,但我得到了这个

array(1) { 
     ["my_session"]=> string(88) "a:3:{s:5:"value";s:21:"boogie all night long";s:4:"life";i:300;s:4:"born";i:1431562088;}"
 } 

 string(21) "boogie all night long"
 Array
 (
    [my_session] => a:3:{s:5:"value";s:21:"boogie all night long";s:4:"life";i:300;s:4:"born";i:1431562088;}
 )

但是当我注释掉创作部分时,它会返回

bool(false)
Array
(
)

0 个答案:

没有答案