0
我正在使用zend framework-2做项目。我想限制通过URL访问某些Web目录(如果用户没有登录)。如果用户尝试登录,我需要重定向登录页面。这是我所做的(粗略的想法不是代码)。我想限制通过URL访问某些Web目录
AuthController
if(username and password ok){
setcookie('username', $username);
setcookie('password', $password);
//redirect to AlbumController indexAction
return $this->redirect()->toRoute('album', array('controller' => 'album', 'action' => 'index'));
}
左右的时间内AlbumControoler我加入这个代码
public function indexAction()
{
if(isset ($_COOKIE['username']) && isset ($_COOKIE['password'])){
//some codes
}
else{
//if user not logged redirect to loogin page
return $this->redirect()->toRoute('auth', array('controller' => 'auth', 'action' => 'index'));
}
}
所以如果我用这种方式,我需要每一个动作上面添加代码。所以我的问题是这样好吗?或者是他们简单的方法来做到这一点?
千万不要使用用户名/密码格式的Cookies。不仅可以手动创建Cookie(我可以轻松访问您的网络应用程序),但它们也是这样的巨大安全风险!查看[ZfcUser] [1]和[ZfcAcl] [2] [1]:https://github.com/ZF-Commons/ZfcUser [2]:https://github.com/ZF-Commons/ZfcAcl – Sam
@Sam可以使用会话而不是cookies – user1784592
您需要了解差异。看到以下问题的一些见解:http://stackoverflow.com/questions/356562/web-authentication-state-session-vs-cookie-vs和就ZF2而言,上述模块将为你做相当多的事情;) – Sam