简体   繁体   中英

How to check if user is logged with Zend Framework?

I'm learning Zend Framework, but I have some doubts about the usage and the concepts.

I want to check if the user is logged to allow access to all the pages. If it is, show the page, if not, display the login the page.

My main doubts are what I need to use to do this (Zend_Auth, Zend_Acl, etc) and where to check if the user is logged (in each controller or the framework automatically checks this for each requisition).

The tool you want to use is Zend_Auth which is quite easy to use when you get the hang of it.

Checking if a user is logged in can be as simple as:-

$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()) $loggedIn = true;

See Rob Allen's excellent tutorial on getting started with Zend Auth .

The method I use is to set up a user class that looks after authorisation and access control and inject it into my application as an Action Helper , so that in any of my controllers I can just do:-

$this->user->checkSomething();

The authorisation part needs to affect all the parts of your site which you don't want public and each affected controller needs to check that the user is logged in. For access control, that is done on a per role/per resource basis depending on how fine grained you need to be.See ACL and AUTH in the manual.

Want to check if the user is logged in ZendFramework? Try this:

Place this in anywhere of your controller to 'debug', and put it in the top or beginning of your code:

if (Zend_Auth::getInstance()->hasIdentity()) echo "oh yeah I'm logged in lol"; die;

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