Q. PHP Session variable value is not transfering?
So, you are having trouble with php session variables! Well, Let see how can it being solved! If you didn't read about PHP Session variables then have a look on the manual!
Well let see what is happening...
PHP file: <test1.php>
<?PHP session_start(); header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json; charset=UTF-8"); //some code to determine $approvedToSet $approvedToSet = true; if(isset($approvedToSet)) { $_SESSION["userName"] = 'MyName'; echo " Session variable is set." .$_SESSION["userName"]; } else { echo "User name is not set";} ?>
PHP file: <test2.php>
<?PHP echo $_SESSION["userName"];
ERROR:
Notice: Undefined index: userName in /var/www/xyz on line 30
[Solution:]
Correct Your file and ensure session_start(); function is on the top of your page.
PHP file: <test2.php>
<?PHP session_start() echo $_SESSION["userName"]; ?>
- Stop your apache2 or http server and start it again!
- Ensure session.save_path has been set.
- Must be Make sure
session_start();
is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening<?php
tag before anything else. Also ensure there are no whitespaces/tabs before the opening<?php
tag. - After the
header
redirect, end the current script usingexit();
(Others have also suggestedsession_write_close();
andsession_regenerate_id(true)
, you can try those as well, but I'd useexit();
). - Make sure cookies are enabled in the browser you are using to test it on.
- Ensure
register_globals
is off, you can check this on thephp.ini
file and also usingphpinfo()
. Refer to this as to how to turn it off. - Make sure you didn't delete or empty the session.
- Make sure the key in your
$_SESSION
superglobal array is not overwritten anywhere. - Make sure you redirect to the same domain. So redirecting from a
www.yourdomain.com
toyourdomain.com
doesn't carry the session forward. - Make sure your file extension is
.php
(it happens!). - If you use a connection script, dont forget to use
start_session();
at the connection too, had some trouble before noticing that issue.
iu
No Comment to " [SOLVED] PHP Session variable value doesn't retrieved to another page "