Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:34:25+00:00 2026-05-28T17:34:25+00:00

I want to know how to use PHP session variable in zend framework here

  • 0

I want to know how to use PHP session variable in zend framework

here is my code so far :-

 public function loginAction()
    {
        $this->view->title = 'Login';
        if(Zend_Auth::getInstance()->hasIdentity()){
            $this->_redirect('index/index');
        }

            $request = $this->getRequest();
            $form = new Default_Form_LoginForm();
            if($request->isPost()){
            if($form->isValid($this->_request->getPost())){
                $authAdapter = $this->getAuthAdapter();

                $username = $form->getValue('username');
                $password = $form->getValue('password');

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

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

                if($result->isValid()){
                    $identity = $authAdapter->getResultRowObject();

        print_r($authAdapter->getResultRowObject());

                    $authStorage = $auth->getStorage();
                    $authStorage->write($identity);

        echo $authAdapter->getIdentity() . "\n\n";

           //         $this->_redirect('index/index');
                } else {
                    $this->view->errorMessage = "User name or password is wrong.";
                }
            }
        }


        $this->view->form = $form;

     }

now i want to store username in session and i want to use in some other page like

echo "welcome," .$this->username; what i can do ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:34:26+00:00Added an answer on May 28, 2026 at 5:34 pm

    Instead of writing $identity to $authStorage, you can store a custom object or a model.

    Here is an example:

    <?php
    
    class      Application_Model_UserSession
    implements Zend_Acl_Role_Interface
    {
        public $userId;
        public $username;
    
        /** @var array */
        protected $_data;
    
        public function __construct($userId, $username)
        {
            $this->userId   = $userId;
            $this->username = $username;
        }
    
        public function __set($name, $value)
        {
            $this->_data[$name] = $value;
        }
    
        public function __get($name)
        {
            if (array_key_exists($name, $this->_data)) {
                return $this->_data[$name];
            } else {
                return null;
            }
        }
    
        public function updateStorage()
        {
            $auth = Zend_Auth::getInstance();
            $auth->getStorage()->write($this);
        }
    
        public function getRoleId()
        {
            // TODO: implement
            $role = 'guest';
            return $role;
        }
    
        public function __isset($name)
        {
            return isset($this->_data[$name]);
        }
    
        public function __unset($name)
        {
            unset($this->_data[$name]);
        }
    }
    

    Now in your login controller you can do:

    if($result->isValid()){
        $identity = new Application_Model_UserSession(0, $username); // 0 for userid
    
        // You can also store other data in the session, e.g.:
        $identity->account = new Account_Model($authAdapter->getResultRowObject());
    
        $identity->updateStorage(); // update Zend_Auth identity with the UserSession object
    

    Typically, I have an account object that I also store in the UserSession object, and make easy access to the username and userId via public properties.

    Now at any time you can get the object:

    $identity = Zend_Auth::getInstance()->getIdentity(); // Application_Model_UserSession
    

    Just don’t forget to make sure it is a Application_Model_UserSession.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a php session variable as a parameter when I call
I use the PHP language for file upload and want to know that the
I want to know how to use variables for objects and function names in
I want to know how and when can I use the exit() function like
i want to know what wordpress build-in function you will use when sanitizing specific
I have a php variable echo $id. Now I want to use the $_POST
I want to know what is the use of .properties file and where it
i want to know that when we want to use push notification service.From where
I want to use push notification in my app.i want to know that do
I Want to know which one is preferred while coding to use Static Methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.