DaedBaet
DaedBaet

Reputation: 439

PHP session does not work from page to page

To login I use:

<?php
session_start();

if($_POST){
$csUSER='USERNAME';
$csPASS='PASSWORD';
$user=$_POST['user'];
$pass=$_POST['pass'];
if ($user==$csUSER) {
    if ($pass==$csPASS){
        $_SESSION['cdb']="1";
        header("Location: /");
        exit;
    } else {
        $passerror='<span class="errormsg">Wrong Password.</span>';
    } // END IF PASSWORD
} else {
$usererror='<span class="errormsg">Wrong Username.</span>';
} // END IF USERNAME
} // END IF $_POST
?>

To allow myself to do admin tasks per page (included in all pages [top of page]):

<?php
session_start();

if(isset($_SESSION['cdb'])){  
$loggedn="WORD";
}
?>

This allows me to:

<?php
if ($loggedn=="WORD") { WHATEVER }
?>

And to make sure I only have access to backend pages when logged in (included in all backend pages):

<?php
// backend login check
if($loggedn!="WORD") {
header("Location: /"); // if not logged in, go to homepage
exit;
}
?>

The problem is, it works perfect on my pc, but I have another pc my wife uses for data collation and it does not stay logged in on her pc. We both use Linux (Fedora) with FF. I have been over ever line of code in each page, help!

Upvotes: 0

Views: 3510

Answers (2)

George Cummins
George Cummins

Reputation: 28906

A few things to check:

  1. Ensure that you are starting with a clean slate. Clear cache and cookies in your browser to ensure that you don't have an old session open.
  2. Ensure that session data is being stored on the new machine. Session data is commonly stored in /tmp
  3. Ensure that there is no client-specific code being executed in relation to the session.

Upvotes: 1

martinstoeckli
martinstoeckli

Reputation: 24071

Call the exit function after redirecting to another page, otherwise the following code will be executed anyway, what can lead to strange behaviour.

if($loggedn != "WORD")
{
  // redirect to login page
  header("Location: login.php");
  exit;
}
// the following code will be executed if exit is not called
...

Upvotes: 0

Related Questions