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:

  1. user logs in @ index.html, form submits login.php;
  2. login.php basic sanity, isset , empty checks, checks credentials database. if email address , password correct (i.e., exist in database) put them in $_session variable , redirect user home.php.
  3. home.php retrieves $_session variables. 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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -