tags:

views:

274

answers:

3

I have a template file that contains all my header, footer and common information. It includes the appropriate content for the current page (two-step view pattern).

I am trying to set up a login system using PHP Session variables. I can set the variable and sometimes they work but sometimes they disappear. Clicking on links will sometimes make them come back.

My site

Login with

username: test password: test

There are var_dumps of session_id and $_SESSION at the top.

Click on Home. If the session variables disappear click on home (might take as many as 10 times) to see the session information come back. Click on the other navigation and sometimes the session info sticks around and sometimes it doesn't.

Here is the session code at the top of my template file.

<?php
session_start();

require './classes/DBInterface.php';
$db = new DBInterface();

if($_REQUEST['submit']  == 'Login') {
    $username=$_POST['username'];
    $password=$_POST['password'];

    echo '-- login -- '.$username;
    $rs = $db->verify($username,$password,"admin",0);
    $admin = $rs->current();
    if ($rs->valid()) {
     $_SESSION['username'] = $username;
    }
}

echo ' -- session id -- ';
var_dump(session_id());
echo ' -- session var -- ';
var_dump($_SESSION);

I am using PHP5.

+2  A: 

If you are using a load-balanced setup, it could be that only 1 of the N servers has the correct session-data.

By default session-data is stored on the filesystem.
Per session a file is stored in /tmp/ and starts with "sess" followed by the session_id

Bob Fanger
+1  A: 

You're absolutely positive there isn't anything ever being called prior to this? I know session_start() modifies the headers, and other than that, not sure why this isn't working.

Go ahead and turn on error reporting at the top of the script, right before the session_start() call, and see how that helps track this down:

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors',1);
session_start()
Josh
+2  A: 

Hi,

If you are using startlogic (seem you are ?) for your hosting, did you try doing what they say in their FAQ : http://www.startlogic.com/knowledgebase/read_article.bml?kbid=600

They indicate this :

To run PHP sessions, include the following code at the top of any PHP script that uses sessions: session_save_path("your home directory path"/cgi-bin/tmp); session_start();

Maybe this'll help ? Especially if they are using some kind of load balancer, which balances /tmp, but not your home directory ?

Pascal MARTIN
Thanks. It never occurred to me it could be a hosting thing. Now I get to put back all the code I tore out trying to simplify my problem.
Emily
Well, it was an interesting problem ^^Have fun :-)
Pascal MARTIN