Change Zend_Auth storage backend $_SESSION to Memcached
Asked Answered
L

2

20

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();
}
Leptospirosis answered 28/8, 2012 at 13:5 Comment(8)
Ok It turned out that our varnish servers are reason of the problem. Simple piping to url is fixed it.Leptospirosis
I wouldn't store session data to memcached, there is no guarantee that it stays there.Tanga
ok i see the concern, what would you use for that kind of problem? Let say 4 web servers need to share session data..Leptospirosis
Couple of possible solutions: 1) load balanced and sticky sessions - cookie with server id added to request - then every server manages own sessions 2) store session into database 3) store session data to NFS - shared storage for all servers 4) store all necessary session data to cookieTanga
is the cookie safe? how about the performance if hold it in db? shared storage make sense.. Thanks for the comments man!Leptospirosis
Store session to memache with write-thru to a database in order to reliably share session data.Lowbrow
Have you tried getting the output from memcache? What happens when you query memcache with the user's id or whatever is the key?Trussell
Storing the session in Memcache is not the problem. Storing "only" in Memcache or "relying" on Memcache for your session is the problem. I would combine writing the session in the database and in Memcache so I'm sure I can retrieve it if it's kicked out. You would then check in Memcache and if it's not there, check in your database to get the session.Hyacinthus
C
1

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

Cephalometer answered 16/9, 2014 at 20:23 Comment(0)
P
1

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

Phene answered 15/1, 2015 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.