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?
- 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.- after
header
redirect, end current script usingexit();
(others have suggestedsession_write_close();
,session_regenerate_id(true)
, can try well, i'd useexit();
).- make sure cookies enabled in browser using test on.
- ensure
register_globals
off, can check onphp.ini
file , usingphpinfo()
. refer this how turn off.- make sure didn't delete or empty session.
- make sure key in
$_session
superglobal array not overwritten anywhere.- make sure redirect same domain. redirecting
www.yourdomain.com
yourdomain.com
doesn't carry session forward.- make sure file extension
.php
(it happens!).
Comments
Post a Comment