会话不存储变量


Session not storing variable

我的问题是,当调用包含开关控件的静态方法时,没有存储$_SESSION变量。下面是我的代码:

class Booking {
static public function bookCourse() {        
$smarty = SmartySingleton::getInstance();
session_start();
    if (isset($_POST['step'])) {
        switch ($_POST['step']) {
            //step 1 post
            case 1:
                //validate                     
                $errors = $validator->validateStep1();                    
                if ($errors == ""){
                    $_SESSION['step1'] = 'test';
                    var_dump($_SESSION); //here it prints test
                    $smarty->display('step2.tpl');
                }else{
                    $smarty->assign('errors', $errors);
                    $smarty->display('step1.tpl');
                }
                break;
            //step 2 post
            case 2:                    
                $errors = $validator->validateStep2();
                if ($errors == ""){
                    var_dump($_SESSION); //here it prints empty array
                    $_SESSION['step2'] = $_POST;   
                    $smarty->display('step3.tpl');
                }else{
                    $smarty->assign('errors', $errors);
                    $smarty->display('step2.tpl');
                }
                break;
           ....

有谁知道是什么问题吗?谢谢你!

您必须使用session_start()命令启动会话。

cf: http://php.net/manual/en/function.session-start.php

注意:session_start()应该在脚本中尽可能早地出现。

在情况1中,您已经分配了$_SESSION['step1'] = 'test';,因此它打印test。而在情形2中,Nothing被赋值