0

I wrote this script. It is working, but when I leve the page, or when I return to the page, sessions are not being saved. What I am doing wrong?

login.php

<?php
session_start();
require 'classes.php';

//if user is not yet authorized
if ($_SESSION['user']->iduser == null) {
    $dbhandle = mysql_connect($hst, $usr, $pass);
    if (!$dbhandle) {
        die('Could not connect: ' . mysql_error());
    }
    $selected = mysql_select_db($dbName, $dbhandle)
            or die("Could not select the database");

    $result = mysql_query(" query ");
    $resultArray = mysql_fetch_array($result);
    $num_rows = mysql_num_rows($result); //count how many rows returned
    //if 0 rows returned, then login was failed
    if ($num_rows == 0) {
        die('Error! Access denied. Incorrect username or password.');
    } else { //if username and password match
        $_SESSION['user'] = new user();
        $_SESSION['user']->iduser = $resultArray{'iduser'};
        $_SESSION['user']->fname = $resultArray{'fname'};
        echo 'Dear ' . $_SESSION['user']->fname . ', you succesfully logged in!';
    }
    //if user is already logged in
} else {
    echo 'You already logged in!<br>Your email is: ';
}
?>

class user

<?php
class user {
    public $iduser = null;
    public $fname = null;
    public $lname = null;
    public $email = null;
    public $password = null;
    public $permission = null;
}
?>
Andrew
  • 7,619
  • 13
  • 63
  • 117
  • 1
    possible duplicate of [PHP Session data not being saved](http://stackoverflow.com/questions/155920/php-session-data-not-being-saved) – hjpotter92 Sep 13 '12 at 02:31
  • @Giant of a Lannister, do not spam the thread; if you have nothing to say- do not answer. – Andrew Sep 13 '12 at 02:41

1 Answers1

3

You need to "require (classes.php)" before the session starts.

(Simple answer :) )

Reason: the session is decoded when you call session_start. For an object to be unserialized correctly, then the class must be already defined. Ergo you need to define the class before session_start - swapping those two lines around should do it.

http://php.net/manual/en/oop4.serialization.php

Robbie
  • 17,605
  • 4
  • 35
  • 72
  • Another question- what about putting the session inside the class? It is shared across all files; therefore all files require it? – Andrew Sep 13 '12 at 02:43
  • 1
    That's your call on programming logic and flow. Never write something twice, but either write a function call, an include or similar is one rule, but not all your pages may need the session? So I can't really answer that. Note: unscrambling classes each and every page may add unnecessary overhead - so keep classes small and tidy if they are in the session. – Robbie Sep 13 '12 at 02:46
  • Oh - just a warning, even if you are not going to use "user", you still need to declare the class structure before session_start. Otherwise another page, when re-writing the session, may mess it up. That might help with your thinking process. – Robbie Sep 13 '12 at 02:49