Zend_auth in Zend framework (access authentication and persistent identity)

Source: Internet
Author: User
Tags zend framework

After running ZF for a while, I found that there was very little Chinese information, which is relatively more comprehensive than that of the ZF Chinese mobile phone, but some key things have not improved. After studying this theory for a day, I finally put it into practice and found that ZF is much more complex and powerful than I imagined.

Here, we will discuss about Database Authentication and identity persistence. in the manual, We will write the persistence process in a relatively small way. We can only mention the retrieval of persistent identities, I also blame myself for being careless and not paying too much attention to it. So I want to help others. In the evening, I wrote a class for this authentication and persistence, as shown below:

 

Class active_auth
{
Public static $ name = 'adminlogin ';

Public static function login ($ username, $ password)
{
$ Auth = zend_auth: getinstance ();
$ Auth-> setstorage (New zend_auth_storage_session (active_auth: $ name ));
$ Db = zend_registry: Get ('db ');
$ Authadapter = new zend_auth_adapter_dbtable ($ db, 'zf _ admin', 'ausername', 'apassword', 'md5 (?) ');
$ Authadapter-> setidentity ($ username );
$ Authadapter-> setcredential ($ password );
$ Result = $ auth-> authenticate ($ authadapter );
If ($ result-> isvalid ()){
$ Storage = $ auth-> getstorage ();
$ Storage-> write ($ authadapter-> getresultrowobject ());
Return true;
} Else {
Return false;
}
}
Public static function isvalid ()
{
$ Auth = zend_auth: getinstance ();
$ Auth-> setstorage (New zend_auth_storage_session (active_auth: $ name ));
Return $ auth-> hasidentity ();
}
Public static function getidentity ()
{
$ Auth = zend_auth: getinstance ();
$ Auth-> setstorage (New zend_auth_storage_session (active_auth: $ name ));
Return $ auth-> getidentity ();
}
Public static function logout ()
{
$ Auth = zend_auth: getinstance ();
$ Auth-> setstorage (New zend_auth_storage_session (active_auth: $ name ));
$ Auth-> clearidentity ();
}
}

 

After this class is introduced, you can use static access methods to achieve login, identity acquisition, and login logout.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.