dongpu2694 2015-01-16 19:41
浏览 81

PHP Session没有持久化(MVC,不使用Javascript)

I've read a few questions already, and the answers don't seem to relate to my exact problem. My project is using the MVC pattern and Bootstrap. Index.php calls the invoke() function in Controller.php, which in turn includes my views depending on the actions.

My login button does the following:

case "login":
            $name = $_POST['name']; $pass = $_POST['password'];;
            $result = $this->model->loginUser($name,$pass);
            if($result == 1){
                session_start();
                $_SESSION["username"]=$name;
                $_SESSION["lang"]="";
                include 'view/session.php';
            }
            else{
                include 'view/login.php';
            }
            break;

It has access to the $_SESSION["username"] variable, which I use to say Welcome to.

Session and login PHP files consist in a bootstrap navbar. Logged in users have access to more options, like downloading files and viewing their profile. After trying to click on the profile or download buttons, that I've set the same way as the login one, with different values, obviously:

<form class="navbar-form" method="POST" action="index.php">
    <button type="submit" class="btn btn-success" name="action" value="login"><?php echo LOGIN_TXT; ?></button>
</form>

In the Profile action, it does the following:

case "profile":
            include 'view/session.php';
            include 'view/profile.php';
            break;

Not sure if this way is viable, but I've tried to implement it through "layers". Session and Login are the navbar layers, and the body is composed of the views like Profile or Download.

The issue here is that when the session view is called again, it ceases to have access to the variable, returning the following:

Welcome, Notice: Undefined variable: _SESSION in /(...)/mvc/view/session.php on line 44

What would possibly make the session die? I'm at a loss here.

How would I go on about using Javascript or even Ajax to make my sessions persistent? Is this even possible?

  • 写回答

1条回答 默认 最新

  • dqteh7347 2015-01-16 20:36
    关注

    Is it calling session_start();? You need to do that every time before you can access any session variables.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集