I have a PHP code which starts a session using session_start()
. Well, after a user is logged in, is brought to profile.php, which shows that user info. But when the user reloads the page, the session is gone. Is there any way I can maintain it an hour for example? I've tried cookies but I don't know how to tell PHP that the session is started already. Thanks!
Profile.php code for cookies and session start:
if(isset($HTTP_COOKIE_VARS['session'])){
session_start();
} else {
header('Location: index.php');
}
Login code:
session_start();
$_SESSION['pass'] = $password;
header('Location: ../profile.php');
setcookie("session","1",time()+3600,"/");
Code which checks the session:
if($_SESSION['pass'] == $tableArray[0]['password']) {
$username = $tableArray[0]['name'];
$avatar = $tableArray[0]['avatar'];
} else {
header('Location: index.php');
}
I was calling session_destroy() on here:
<li><a href=<?php session_destroy(); echo "index.php"?>>Logout</a></li>
And forgot PHP runs before HTML :P that was the problem!