简体   繁体   English

应用并在不同的控制器中调用会话

[英]apply and call a session in different controllers

I am using zend framework. 我正在使用zend框架。 I have built a simple login screen. 我已经构建了一个简单的登录屏幕。 when the user logs in, I want to set a session and then in the init function of the member area controller its meant to check for the session and grant access, else, redirect to login screen. 当用户登录时,我想设置一个会话,然后在成员区控制器的init函数中,它意味着检查会话并授予访问权限,否则,重定向到登录屏幕。

I have set my login controller like so, this check the username and password and sets the session: 我已经设置了我的登录控制器,这会检查用户名和密码并设置会话:

if (isset($_POST['loginSubmit']) && $form->isValid($_POST)){
            $inputtedUsername = $_POST['username'];
            $inputtedPassword = $_POST['password'];
            if ($inputtedUsername == $username && $inputtedPassword == $password) {
                $loggedIn = new Zend_Session_Namespace('loggedIn');
                $loggedIn->success;
                $this->_forward('index', 'home');
            } else {
                echo 'invalid';
            }
        }

I have a home controller, which only logged in users should be able to see, so in the innit function I have: $loggedIn = new Zend_Session_Namespace('loggedIn'); 我有一个家庭控制器,只有登录用户应该能够看到,所以在innit函数中我有:$ loggedIn = new Zend_Session_Namespace('loggedIn');

if (isset($loggedIn->success)) {
            echo 'success';
        }else{
            $url = $this->view->url(array(
                'controller' => 'index',
                'action' => 'index'));
            header('Location:' . $url);
        }
    }

when i log in, using the correct credentials, it redirects me to login screen as stated in the else function. 当我使用正确的凭据登录时,它会将我重定向到登录屏幕,如else功能中所述。

what am i doing wrong? 我究竟做错了什么?

First your use of Zend_Session_Namespace is incomplete (you never assigned a value to the namespace): 首先,您对Zend_Session_Namespace的使用是不完整的(您从未为命名空间分配值):

$loggedIn = new Zend_Session_Namespace('loggedIn');//here you created a namespace
$loggedIn->success;//here you created a key in that namespace with no value

The way your code seems to be structured any value assigned to $loggedIn->success would return true, so maybe try : 你的代码似乎被构造为分配给$loggedIn->success任何值的方式将返回true,所以可能尝试:

$loggedIn = new Zend_Session_Namespace('loggedIn');//here you created a namespace
$loggedIn->success = true;

While this might fix your current issue, I want to suggest you take a look at two Zend components that can really help with authentication and authorization. 虽然这可能会解决您当前的问题,但我建议您查看两个可以真正帮助进行身份验证和授权的Zend组件。

The first is Zend_Auth , a component that deals with application authentication and will also help handle user session persistence. 第一个是Zend_Auth ,一个处理应用程序身份验证的组件,还将帮助处理用户会话持久性。 Rob Allen has a tutorial to help get you started. Rob Allen有一个帮助你入门的教程

The second is Zend_Acl , The Access Control List component, deals with authorization, who has access to what. 第二个是Zend_Acl ,访问控制列表组件,处理授权,谁有权访问什么。 A place to start with Zend_Acl 一个从Zend_Acl开始的地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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