3

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!

pmerino
  • 5,900
  • 11
  • 57
  • 76
  • Is your session name 'session'? You didn't set its name to 'session' using `session_name('session')` in your login code. By default the session name (and cookie name) is `PHPSESSID` – Michael Berkowski Jul 19 '11 at 13:19

5 Answers5

3
  • you're not calling session_start() again or
  • you're calling session_destroy(); anywhere in file
genesis
  • 50,477
  • 20
  • 96
  • 125
  • 1
    then you call it ALWAYS. PHP is executed on the server, befor the user even see the link :-) – Rufinus Jul 19 '11 at 13:18