调用成员函数,导致致命错误

时间:2015-02-14 06:29:04

标签: php mysql

当我尝试调用类Session时,我收到致命错误。我不知道,哪里可能是一个问题(谷歌搜索大约一个小时),所以如果有人会找到它,请,我会非常感激。 错误

Fatal error: Call to a member function generateRandID() on a non-object in C:\xampp\htdocs\engine\session.php on line 35

    <?php
if (file_exists('install'))
    header("Location: install/");

include_once("game/engine/database/connection.php");
include_once("engine/config.php");
include_once("engine/database/db_MYSQL.php");

include_once ("engine/generator.php");

class Session {
    private $time;
    var $referrer, $url;
    var $logged_in = false;
    public $userinfo = array();

    function Session() {
        global $database;
        $this->time = time();
        session_start();

        $this->logged_in = $this->checkLogin();

        if(isset($_SESSION['url']))
            $this->referrer = $_SESSION['url'];
        else
            $this->referrer = "/";
        $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
        $this->SurfControl();
    }

    public function Login($user) {
        global $database, $generator;
        $this->logged_in = true;
        $_SESSION['sessionId'] = $generator->generateRandID();
        $_SESSION['username'] = $user;

        $database->updateUserField($_SESSION['username'], "sessionId", $_SESSION['sessionId'], 0);
        header("Location: index.php");
    }

    public function Logout() {
        global $database;
        $this->logged_in = false;
        $database->updateUserField($_SESSION['username'], "sessid", "", 0);
        if(ini_get("session.use_cookies"))
            $params = session_get_cookie_params();
        session_destroy();
        session_start();
    }

    private function checkLogin() {
        global $database;
        if (isset($_SESSION['username']) && isset($_SESSION['sessionId'])) {
            if (!$database->checkActiveSession($_SESSION['username'], $_SESSION['sessionId'])) {
                $this->Logout();
                return false;
            }
            else {
                //update database
                $database->updateUserField($_SESSION['username'], "isOnline", 1, 0);
                return true;
            }
        }
        else
            return false;
    }

    private function SurfControl() {
        if (SERVER_WEB_ROOT)
            $page = $_SERVER['SCRIPT_NAME'];
        else {
            $explode = explode("/", $_SERVER['SCRIPT_NAME']);
            $i = count($explode) - 1;
            $page = $explode[$i];
        }
        $pagearray = array("overview.php", "logout.php", "inventoryswap.php", "shop.php", "map.php", "messages.php", "training.php", "work.php", "ajax.php", "packageswap.php", "report.php", "location.php");
        $pagearray1 = array("index.php");
        if(in_array($page, $pagearray1) && $this->logged_in)
            header("Location: game/overview.php");
        else if (in_array($page, $pagearray) && !$this->logged_in)
            header("Location: index.php");
    }
};

$session = new Session;

&GT;

2 个答案:

答案 0 :(得分:0)

错误即将发生,因为函数generateRandID()是在不是对象的变量上调用的。

这意味着首先必须通过将构造函数赋值给变量来使其成为对象。例如:

$generator = new SomeClass();

并确保SomeClass应该定义您正在调用的函数。

答案 1 :(得分:0)

你确定你正在为函数生成id生成函数generateRandID(){}