为什么我的变量显示NULL


Why is my variable showing NULL?

所以我有一个用户类,代码如下:

class User {
private $ID;
private $userLevel;
private $username = "";
private $password = "";
private $lastHit;
/**
 * Sets the username.
 * @param string $username - The username you want to change too.
 * @return string - Returns the username which was set.
 */
public function setUsername($username) {
    return $this->username = $username;
}
/**
 * Sets the password.
 * @param string $password - The password you want to change too.
 * @return string - Returns the password which was set.
 */
public function setPassword($password) {
    return $this->password = $password;
}
/**
 * Sets the ID.
 * @param int $ID - The ID you want to change too.
 * @return int - Returns the ID which was set.
 */
public function setID($ID) {
    return $this->ID = $ID;
}
/**
 * Sets the User Level.
 * @param int $userLevel - The User Level you want to change too.
 * @return int - Returns the User Level which was set.
 */ 
public function setUserLevel($userLevel) {
    return $this->userLevel = $userLevel;
}
/**
 * Returns the username stored in $username.
 * @return string - Returns the username stored in $username.
 */
public function getUsername() {
    return $this->username;
}
/**
 * Returns the password stored in $password.
 * @return string - Returns the password which has been set.
 */
public function getPassword() {
    return $this->password;
}
/**
 * Returns the ID stored in $ID.
 * @return int - Returns the ID which has been set.
 */
public function getID() {
    return $this->ID;
}
/**
 * Returns the User Level stored in $userLevel.
 * @return int - Returns the User Level which has been set.
 */
public function getUserLevel() {
    return $this->userLevel;
}
/**
 * Returns the Last Hit stored in $lastHit.
 * @return date - Returns the time of the users last hit.
 */
public function getLastHit() {
    return $this->lastHit;
}
/**
 * Checks if the provided $username and $password exist in `users` table in DB. Used for login authentication.
 * @global object $PDO - Connection for DB.
 * @param string $username - The username which you would like to check.
 * @param string $password - The password which you would like to check.
 * @param error'Error $errors - Object class for the Error class.
 * @return int - Returns 1 if user is found 0 if not.
 */
public function checkLogin(error'Error $errors, $username, $password) {
    global $PDO;
    $checkUser = $PDO->prepare("SELECT COUNT(*) FROM `users` WHERE Username=? AND Password=?");
    $checkUser->bindParam(1, $username, PDO::PARAM_STR);
    $checkUser->bindParam(2, $password, PDO::PARAM_STR);
    $checkUser->execute();
    $rowCount = $checkUser->fetch(PDO::FETCH_NUM);
    if($rowCount[0] == 1) {
        session_start();
        $selectLoggedInUser = $PDO->prepare("SELECT * FROM `users` WHERE Username=?");
        $selectLoggedInUser->bindParam(1, $username, PDO::PARAM_STR);
        $selectLoggedInUser->execute();
        $results = $selectLoggedInUser->fetch(PDO::FETCH_OBJ);
        $this->setID($results->ID);
        $this->setUserLevel($results->User_Level);
        $this->setUsername($results->Username);
        $_SESSION['ID'] = $this->getID();
        $_SESSION['User_Level'] = $this->getUserLevel();
        $_SESSION['Username'] = $this->getUsername();
        $_SESSION['Online'] = 1;
        $this->lastHit = $_SESSION['Last_Hit'] = date('g:i:s A');
        return 1;
    } else {
        $errors->setError("Username or Password incorrect.");
        return 2;
    }
}

然后我有一个文件,它设置了登录页面的详细信息:

<?php
        if($users->checkLogin($errors, $username, $password) == 1) {
            $functions->message("You have logged in!", "success");
            $functions->redirect('loggedIn', 'timed', 3);
        } else {
            $functions->message($errors->getError(), "errors");
        }
    }
}

然而,当我尝试访问名为loggedIn.php的页面中的变量时,该页面在用户登录后调用,没有显示任何内容。所以我执行了var_dump();当我调用getter时,NULL被传递。但是,如果我直接通过会话调用,则显示值。

loggedIn.php

<?php
session_start();
/**
 * @author Script47
 * @copyright (c) 2014, Script47
 * @version 1.0
 */
include 'functions/Functions.php';
include 'functions/User.php';
include 'functions/Module.php';
include 'functions/Error.php';
include 'config/pdo.config.php';
$functions = new core'Functions();
$users = new User();
$modules = new module'Module();
$errors = new error'Error();
define('MODULE_NAME', 'Login');
define('MODULE_VERSION', '1.0');
$modules->setModuleName('Login');
$modules->setModuleVersion(1.0);
$modules->setModuleTitle();
if($users->checkSessionExist() == 2) {
    $errors->setError("You need to be logged in to view this page.");
    $functions->message($errors->getError(), "error");
    return;
}
echo '<h1>Auth Page</h1>';
echo var_dump($users->getID());
$functions->lineBreak();
echo $users->getUsername();
$functions->lineBreak();
echo $users->getLastHit();

我试过调试,但无济于事,我转向StackOverFlow,我看了看谷歌,但找不到解决方案。

在您的脚本(loggedIn.php)中也有$users = new User();行。这一行用一个新的空对象覆盖在include文件中初始化的users对象。

所以,从loggedIn.php中删除这一行(从另一个文件中删除而不是),您应该没问题。