session - PHP $_SESSION variable not working first time -
$_session['isloggedin']
doesn't seem working on first load.
happens on server, not on localhost.
session_start() @ top of each page.
initialized to: $_session['isloggedin'] = false;
when user logs in $_session['isloggedin'] = true;
when user logs out $_session['isloggedin'] = false;
on home.php:
if (!$_session['isloggedin']) { die(header("location: login.php")); }
on login.php:
if ($_session['isloggedin']) { die(header("location: home.php")); }
when login , sent home page $_session['isloggedin']
doesn't seem true redirects login.php. since true redirects home.php causing redirect loop.
when redirect loop error pops up, refresh , taken right page. page self refreshes , takes me correct page, still showing redirect error before.
why isn't $_session variable working on server? correct value doesn't seem register first time on every page, every site link.
edit:
everything works expected on localhost not on online server.
when login clicked , passes class login function called:
class users { $_session['isloggedin'] = false; function __construct() { if (session_id() == "") { session_start(); } if (isset($_session['isloggedin']) && $_session['isloggedin'] == true) { if (session_id() == '') { session_start(); } } } function login($user,$password) { if (session_id() == "") { session_start(); } $_session['isloggedin'] = false; $mysqli = new mysqli(db_host, db_username, db_password, db_database); if ($mysqli->connect_errno) { return false; } $user = $mysqli->real_escape_string($user); $password = $mysqli->real_escape_string($password); $query = "select * users email=$user"; if (!$result = $mysqli->query($query)) { return false; } $row = $result->fetch_assoc(); $db_pass = $row['password']; if (crypt($password,$db_pass) != $db_pass) { return false; } $_session['isloggedin'] = true; if (session_id() == '') { session_start(); } return true; } }
try changing code this
if (!isset($_session['isloggedin'])) { header("location: login.php"); } else { header("location: home.php"); }
Comments
Post a Comment