简体   繁体   中英

cannot access object user from another php page

I'm trying to learn MVC pattern but,even if I'm trying hard, it seems I still got big issues. I have got a controller,named baseController that do the following:

class baseController {

    public $model;
    public $user;
        ...
        $activeuser = $this->model->getlogin();
        if ($activeuser != 'invalid user' && $activeuser != "") {                                                                                               
                            $this->user=$activeuser;
            header("Location:home.php");
        }

I have got a model.php file which contains the getlogin() function:

public function getlogin() {

    if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
        $username = mysql_real_escape_string($_REQUEST['username']);
        $pwd = mysql_real_escape_string($_REQUEST['password']);
        $pwd = md5($pwd);
        $query = mysql_query("SELECT * FROM users WHERE username='$username' AND password ='$pwd' AND attivato =1;");

        if (mysql_num_rows($query) == 1) {
                        require_once 'User.php';
                        $sql=mysql_fetch_array($query);
                        $activeuser = new User();
                        $activeuser->username=$sql['username'];
                        $activeuser->email=$sql['email'];
            return $activeuser;
        } else {
            return 'invalid user'; //TO-DO
        }  
    }
}

The home.php create a new homeController and calls its invoke() function.The homeController file include the view page,that's called afterlogin.php.

In the afterlogin.php I've got the "ERROR":

    if (isset($activeuser)){
    echo "<p>Utente ".$activeuser->username."</p>";
    echo "<p>Email ".$activeuser->email."</p>";}
    //echo "<p>Pass ".$activeuser->pwd."</p>";
    echo"<h1> HOMEPAGE, LOGIN OK </h1>";

It seems the homeController,and so the afterlogin page cannot access the user created in the baseController file. If I try an echo inside the baseController of $this->user->username everything is working. What should I do?? HELP!!

The client-server lifecycle is effectively stateless; on every page load, your variables and objects are wiped out.

There are the client-sourced $_POST and $_GET superglobals, which is part of the standard form submission and url query processes.

The server has databases, file writing (sketchy from a security POV) and the $_SESSION superglobal. These are the ways the server can manage a data state between pageloads.

Understand that if you're using objects, you need to have them instantiated on every page load for them to work. You can store your user_ID in $_SESSION['user_ID'] and instantiate the user object from it every time, making appropriate changes according to how the data changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM