php - Sometimes Session variables stop working -
i've had twice now. out of blue, log-in system stops working, , debugging find out $_session variable not survive log-in process. then, without obvious cause, resumes working. here's flow:
- user logs in @
index.html, form submitslogin.php; login.phpbasic sanity, isset , empty checks, checks credentials database. if email address , password correct (i.e., exist in database) put them in$_sessionvariable , redirect userhome.php.home.phpretrieves$_sessionvariables. here fails.
the second time (a few minutes ago) read more , found forum thread hadn't read previous time happened (i stopped reading when session variables worked again) said need have <?php instead of <? before session_start();. tried it, not expecting work, when logged in, directly after changing (and thing changed afaik) worked. cause found? let's check after changing <?php <?. still works. can cause of , how can prevent (or, if can't prevented, detect what's going on)?
edit:
something interesting: i've got small utility function check if user logged in:
function assertuserlogin() { try { $user = new user($_session['email'], $_session['pwd']); } catch(exception $ex){ writetolog("exception: " . $ex->getmessage()); header("location: http://www.korilu.nl/maurits/anw?requested:" . $_server["request_uri"]); } writetolog($user->email . " logged in\n"); return $user; } so can this:
<? session_start(); $user = assertuserlogin(); ?> on every page user needs logged in. interesting thing here is, if fails (as described above), calls function writetolog() (log() taken php standard library):
function writetolog($string) { $log = fopen("log.txt", "w"); fwrite($log, $string); fclose($log); } which pretty simple. log remains empty. (i sure function writetolog() gets called, because redirected http://www.korilu.nl/maurits/anw?requested:/maurits/anw/home.php. assertuserlogin() function place that.)
try session_write_close(); @ places script ends exit; die(); , page end.
Comments
Post a Comment