Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session is lost after submit

Tags:

php

session

This makes no sense. It works in localhost, but not in my server.

Before submiting the form, if I var_dump() the $_SESSION it retrieves me the following:

array(2) { ["email"]=> string(40) "082b6eff9db5019e6a28f586a679b7f72fab27f4" ["id"]=> int(5) }

The form is this one:

<form method='POST' action='response.php?type=add_customer'>
    <input type='text' name='customer'/>
    <input type='submit' value='add'/>
</form>

If I var_dump() the $_SESSION on response.php I get: array(0) { }

if(!isset($_SESSION)){ session_start(); }
var_dump($_SESSION);

How d'hell is this possible?

Because it works localhost but not in my server..could it be a php.ini problem? If so, what?

EDIT (1): Changed my code to this (in response.php):

session_start();

if(isset($_REQUEST['type'])){
    switch ($_REQUEST['type']){
       case 'add_customer': 
        var_dump($_SESSION);
        break;
    }
}

Continues not to work. It retrieves an empty array.

EDIT (2): Solved. If someone could explain to me why this:

session_start();
var_dump($_SESSION); // doesn't work
print_r($_SESSION); // doesn't work
echo $_SESSION['id']; // works

I appreciate. Because the problem was this.

like image 578
user3243925 Avatar asked Feb 22 '26 09:02

user3243925


1 Answers

Put session_start(); as the first thing after the <?php tag at the top of every page were you want your $_SESSION data to persist

like image 75
Pwner Avatar answered Feb 25 '26 00:02

Pwner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!