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.