Vika
Vika

Reputation: 391

Session not storing variable

My problem is that when calling a static method which contains a switch control, the $_SESSION variable is not stored. Here's my code:

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;

           ....

Does anyone know what could be the problem? Thank you!

Upvotes: 0

Views: 183

Answers (2)

In case 1 , you have assigned $_SESSION['step1'] = 'test'; so it prints test. Whereas in case 2 , Nothing is assigned.

Upvotes: 0

TimWolla
TimWolla

Reputation: 32691

You have to start your session with the session_start() command.

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

Note: session_start() should occur as early as possible in your script. As soon as something is printed out it will no longer work.

Upvotes: 2

Related Questions