PHP Question

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).

Answer Source

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


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download