为什么我的变量显示为NULL?

时间:2014-05-17 21:52:15

标签: php oop

所以我有一个带有以下代码的用户类:

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,我查看了Google但找不到解决方案。

1 个答案:

答案 0 :(得分:3)

在您的脚本(loggedIn.php)中,您还有一行$users = new User();。此行将使用新的空对象覆盖在包含文件中初始化的用户对象。

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