Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php sessions value get wiped after ajax request

I have a script where i set my uid and email sessions when a user logs in.

in 2 variables:

$_SESSION["uid"] = $user_id;//1,2,3 etc..
$_SESSION["email"] = '[email protected]';

I have a feature where a user can post some data via ajax. But when the data are sent to the server and the request is completed, then my email session's value gets wiped and that causes my user not to be able to do some actions.

I searched my whole script and i couldnt find any

$_SESSION["email"] = '';

after an ajax request is done or wherever else it might be.

any clues?

EDIT: var_dump of _SESSION:

array(2) { ["email"]=> &string(0) "" ["uid"]=> &string(11) "91283921834" }

SOLUTION: My host was showing 5.2.17 as default php version so i had to call

AddType application/x-httpd-php53 .php

in my .htaccess file to make it see 5.3.5 as the live php version which removed the references and now its working fine.

like image 754
stergosz Avatar asked Jan 22 '26 09:01

stergosz


2 Answers

You somewhere assign a variable by reference to your session, you can see this with the & character in your var_dump:

array(2) { ["email"]=> &string(0) "" ["uid"]=> &string(11) "91283921834" }
#                     ^^^                     ^^^

A reference in PHP means, that in your case $_SESSION['email'] has become an alias for some other variable. If that other variable is changed, $_SESSION['email'] will change as well as it's only an alias.

So you not only need to look into your code where you change $_SESSION['email'] but more importantly which parts makes it an alias and where the alias get changed as well.

It's just a guess but search for &$email or just $email. Look for functions that accept parameters by reference (&$parameter), return by reference or are called with parameters by reference.

like image 76
hakre Avatar answered Jan 24 '26 22:01

hakre


You have to call session_start() at the top of each of your script in order to actually create a session

Place it just after the <?php tag

like image 28
fin1te Avatar answered Jan 24 '26 23:01

fin1te



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!