Session variables not working php -


here code of login page login script checks authenticity of user , redirects inbox page using header function.

<?php session_start();  include_once('config.php'); $user=htmlentities(stripslashes($_post['username'])); $password=htmlentities(stripslashes($_post['password'])); // query processing on database      if(($id_user_fetched<=$id_max_fetched) && ($id_user_fetched!=0)){ $_session['loggedin'] = 'yes';     header("location:http://xyz/inbox.php?u=$id_user_fetched");     //echo 'login successful';     }else{         echo 'invalid login';         echo'<br /> <a href="index.html">click here try again</a>';         } }else{     echo mysqli_error("login credentials incorrect!");     } ?> 

the inbox.php page looks this:

<?php session_start();  echo 'session ='.$_session['loggedin']; if($_session['loggedin'] != 'yes'){ echo $message = 'you must log in see page.'; //header('location:login.php'); }  //rest of code  ?> 

now above code, inbox.php shows output: session=you must log in see page. means either session variable not being setup or inbox.php unable retrieve session variable. going wrong?

  1. make sure session_start(); called before sessions being called. safe bet put @ beginning of page, after opening <?php tag before else. ensure there no whitespaces/tabs before opening <?php tag.
  2. after header redirect, end current script using exit(); (others have suggested session_write_close(); , session_regenerate_id(true), can try well, i'd use exit();).
  3. make sure cookies enabled in browser using test on.
  4. ensure register_globals off, can check on php.ini file , using phpinfo(). refer this how turn off.
  5. make sure didn't delete or empty session.
  6. make sure key in $_session superglobal array not overwritten anywhere.
  7. make sure redirect same domain. redirecting www.yourdomain.com yourdomain.com doesn't carry session forward.
  8. make sure file extension .php (it happens!).

php session lost after redirect


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -