douhan5853 2014-08-12 20:10
浏览 23

无法从另一个php页面访问对象用户

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!!

  • 写回答

1条回答 默认 最新

  • duanji9264 2014-08-12 20:47
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?