Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Zend_Auth storage backend $_SESSION to Memcached

I'm trying to change the session backend of Zend_Auth. But couldn't succeed it. In my bootstrap.php ;

    $oBackend = new Zend_Cache_Backend_Libmemcached(
        array(
            'servers' => $servers,
            'compression' => false
    ) );

    // configure caching frontend strategy
    $oFrontend = new Zend_Cache_Core(
        array(
            'caching' => true,
            'automatic_serialization' => true
        ) );

    // build a caching object
    $cache = Zend_Cache::factory( $oFrontend, $oBackend );

    $saveHandler = new \Application\Auth\Adapter\Memcached();
    $saveHandler->setCacher($cache);

    \Zend_Session::setSaveHandler($saveHandler);

It was saving the values Memcache successfully with no problem. I test it ;

    $namespace = new Zend_Session_Namespace();
    $namespace->name = "Fatih";

In other controllers;

    $ns = new Zend_Session_Namespace();
    var_dump($ns->name);

It's ok, but I couldn't see Zend_Auth values in Memcache. But if I var_dump($_SESSION) I can see it like ;

["Zend_Auth"]=> array(1) { ["storage"]=> object(Application_Security_Auth_Storage)#66 (1) { ["_user":protected]=> object(Application_Security_Auth_User)#84 (4) { ["id":protected]=> object(MongoId)#87 (1) { ["$id"]=> string(24) "4fcca6b8c863c79d33000004" } ["username":protected]=> string(5) "admin" ["role":protected]=> string(5) "admin" ["fullname":protected]=> NULL } } }

Here you can see my login method ;

public function login($username, $password)
{
    if ($username == "" || $password == "")
        return false;

    $adapter = new \Application_Security_Auth_Adapter();

    $adapter->setIdentity($username);
    $adapter->setCredential($password);

    $auth = \Zend_Auth::getInstance();
    $result = $auth->authenticate($adapter);

    return $result->isValid();
}
like image 356
Fatih Donmez Avatar asked Aug 28 '12 13:08

Fatih Donmez


2 Answers

I do not know if this would be of any help but, Zend_auth automatically creates the storage thing to which you can access from anywhere using

$session = new Zend_Session_Namespace('Zend_Auth');
$session->storage->//here goes your property like user id password etc

Now if you use Zend_Auth it will use Zend_Auth_Storage_Session default value being "Zend_Auth" as the Zend_Session_Namespace. Now to change the namespace used modify the default value in Zend_Auth_Storage_Session else do it all manually if you want to cache this information or have it stored elsewhere you can always access it and move it to where you want.

Now i hoped I helped but I don't know anythng of memcache

like image 156
zardilior Avatar answered Oct 20 '22 08:10

zardilior


I think it is simplest way and it will work out for you

Dear Use user This Class For Zend Framework For Session.

use Zend\Session\Container;

Then Do The Below procedure For Get Values From The Seesions.

    $user_session = new Container('user_login');
    $loginUser = $user_session->login_user['user_type'];

$user_session->login_user in this variable i store all the array of user related information like user type, user email, user id etc...then i get this session values on each page...

like image 30
Hassan Raza Avatar answered Oct 20 '22 08:10

Hassan Raza