会话没有被保存

时间:2012-09-13 02:19:23

标签: php session

我写了这个脚本。 它正在工作,但当我离开页面时,或当我返回页面时,会话没有被保存。我做错了什么?

的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: ';
}
?>

班级用户

<?php
class user {
    public $iduser = null;
    public $fname = null;
    public $lname = null;
    public $email = null;
    public $password = null;
    public $permission = null;
}
?>

1 个答案:

答案 0 :(得分:3)

在会话开始之前你需要“require(classes.php)”。

(简单回答:))

原因:调用session_start时会话被解码。对于要正确反序列化的对象,必须已定义该类。你需要在session_start之前定义类 - 交换这两行应该这样做。

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