Home / Zend Framework / Create Different session for admin and frontend using zfcuser in Zend framework 2

Create Different session for admin and frontend using zfcuser in Zend framework 2

If you want use twice ZendAuthenticationAuthenticationService, look in ZendAuthenticationStorageSession, you’ll see in __construct, there’s an namespace parameter, change it for “Zend_Auth_Frontend” (in front) and “Zend_Auth_Backend” for the back.

How you can perform above change please follow below steps:
For Frontend Changes Please change in two file
Step 1:
Go to ZfcUsersrcZfcUserAuthenticationAdapterAbstractAdapter.php
Search below code in AbstractAdapter.php, approx line no 23 to 25

Select Code
if (null === $this->storage) {
    $this->setStorage(new StorageSession(get_class($this)));
}


and replace it with

Select Code
if (null === $this->storage) {
    $this->setStorage(new StorageSession('Zend_Auth_Frontend',get_class($this)));
}


Step 2:
Go to ZfcUsersrcZfcUserAuthenticationStorageDb.php
Search below code in Db.php, approx line no 115 to 117

Select Code
if (null === $this->storage) {
    $this->setStorage(new StorageSession);
}


and replace it with

Select Code
if (null === $this->storage) {
    $this->setStorage(new StorageSession('Zend_Auth_Frontend'));
}

For Backend Side(Admin side) Changes Please change in two files :

Step 1:
Go to ZfcUsersrcZfcUserAuthenticationAdapterAbstractAdapter.php
Search below code in AbstractAdapter.php, approx line no 23 to 25

Select Code
if (null === $this->storage) {
    $this->setStorage(new StorageSession(get_class($this)));
}


and replace it with

Select Code
if (null === $this->storage) {
    $this->setStorage(new StorageSession('Zend_Auth_Backend',get_class($this)));
}


Step 2:
Go to ZfcUsersrcZfcUserAuthenticationStorageDb.php
Search below code in Db.php, approx line no 115 to 117

Select Code
if (null === $this->storage) {
    $this->setStorage(new StorageSession);
}


and replace it with

Select Code
if (null === $this->storage) {
    $this->setStorage(new StorageSession('Zend_Auth_Backend'));
}

When your changes done after then you open frontend and backend, now you can login both side with different username and password and you see you are success to Create Different session for Backend (admin) and frontend using zfcuser in Zend framework 2.

About v.shakya

I am V.Shakya, Freelance PHP Programmer. I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than Ten years of experience in vast field of Programming , Designing and Development of websites and various software's.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.