views:

96

answers:

2

How can you see the assigned value of the following SESSION variable?

I run the following after start_session()

$_SESSION['login']['email'] = "ntohuh";

I get after printing with print_r($_SESSION);

( [login] => Array ( [email] => )

This question is based on this thread.

+1  A: 

The value shows up for me. This is what I did, if it helps:

# This empties $_SESSION
$_SESSION = array();

session_start();

$_SESSION['login']['email'] = "ntohuh";

echo '<pre>';
print_r($_SESSION);
echo '</pre>';
Thank you for your for your answer! - I found the bug. I describe it below such that we can avoid it in the future.
Masi
A: 

Reply to this question.

Source of the problem

I found out that the problem was in my handle_login_session.php -file at the very beginning of my index.php. I had this there

The very beginning of my index.php in the file handle_login_session.php

     if( $_SESSION['login']['logged_in'] == false ){
         $random_number = rand(1,100000);
         $session_id = session_id($random_number);
         $_SESSION['login']['email'] = '';               // problem here
 }

I did not think that this could be a problem when making this because I have the following at the very end of my file

The end of my index.php in the file handle_registration.php

 $email = $_POST['login']['email'];     
 $_SESSION['login']['email'] = "ntohuh";               // not effective

I had the idea that the code later in my index.php will overwrite the code before it. However, this is not the case here.

Explanation for the problem

I have one explanation for the strange behaviour. The latter file is called by the following form in my index.php.

 <?php 
     echo ("<form method='post'" 
         . "action='/codes/handlers/handle_registration.php" // called here
         . "'>"
     ); 
 ?>
     <p>Email:                                                                                              
         <input name="login[email]" type="text" cols="92" />
     </p>
     <input type="submit" value="OK" />
 </form>

This action seems to make like an external environment which does not overwrite anything in the main environment.

The following picture summarizes the situation where the order of the files being called is shown.

        handle_login_session.php ----        handler_registration ------
                                    |                                  |
 index.php ----------------------------------------------------------------------->
                                                                               time
Masi