3

I am using Recess Framework for my application & troubled with session handling. I need to store some values in the session.. I am able to create session and session id. But as soon as i navigate to another page, I am not able to retrieve them back !! How to do you people manage your sessions? PHP session handling is simple and straight forward.. but I am simply not able to get this right!

In my home controller I have:

/* !Route GET, / */
function index() {
   .
   .
   session_start();
   $_SESSION['val'] = 'SomeValue';
   .
   .
}

My view displays another form and when user submits, I need to access the session val as well.. so I try to get the value back as:

/* !Route GET, /check */
function check() {
    if(isset($_SESSION['val'])){
       .
       .
       .
    }       
}

But unfortunately, i don't enter the if block .. i am not able to see what my mistake is I tried displaying the session is using session_id() as well and its blank value in check() method.

I also noticed that, all the time I create a new session, the session_id is same!! is it like that in PHP? As far as I read, they should be different!

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133

2 Answers2

1

After many trails and discussion with other community members at Recess framework's forum, here is how I got it working:

I put the session_start() in recess-conf.php and everything seems to work fine for now!

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
0

In my opinion its the best to use a function for starting a session like this:

http://snipplr.com/view/41338/start-a-secure-session/

function startSession($session_name) {
    session_name($session_name);
    $ok = @session_start();
    if(!$ok){
       session_regenerate_id(true); // replace the Session ID
       session_start(); // restart the session (since previous start failed)
    }
}

In your example a second execution of session_start(); is required.

/* !Route GET, /check */
function check() {
    session_start();
    if(isset($_SESSION['val'])){
       .
       .
       .
    }       
}

Or for my example

function startSession($session_name) {
    session_name($session_name);
    $ok = @session_start();
    if(!$ok){
       session_regenerate_id(true); // replace the Session ID
       session_start(); // restart the session (since previous start failed)
    }
}

/* !Route GET, / */
function index() {
   .
   .
   startSession('SID'); // SID is only for example you can use what you want
   .
   .
   $_SESSION['val'] = 'SomeValue';
   .
   .
}


/* !Route GET, /check */
function check() {
       .
       .
       .
    startSession('SID'); // SID is only for example you can use what you want
       .
       .
       .
    if(isset($_SESSION['val'])){
       .
       .
       .
    }       
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
000
  • 280
  • 1
  • 2
  • 11
  • why do you call startSession() in check()? I am trying to access the session that i created in index() or when accessing index.php.. I am sorry to say.. i didn't get your logic,, could you please explain? – Abdel Raoof Olakara Feb 09 '11 at 16:12
  • http://php.net/manual/en/function.session-start.php ... session_start(); starts a session or if a session exist (how it is in this way, because we started in index();), the script will be continue with the exsiting session – 000 Feb 10 '11 at 12:05