0

I am learning Zend Framework and having issues with Zend_Session_Namespace.

Here is the scenario:

  1. Homepage(user clicks on login-Index Controller)
  2. login page(user auth is done->Login Controller)
  3. On Successful login: Create a new zend_Session_Namespace("login") and take him to another page with home page button.
  4. User Clicks the Home Page Button.I can Access the username from the session and display the welcome message.
  5. User again clicks on the login page. I am checking isset($session->name) to prevent login again and take him to other page instead. --> I am failing here . The session is somehow reset , I am quite unsure what I am missing.

    class IndexController extends Zend_Controller_Action
    {
        public function init()
        {
        }
    
        public function indexAction()
        {
             $session = new Zend_Session_Namespace("login_session");
              //Check if the session is already valid
             if(isset($session->name)) {
                $this->view->userLoggedIn="true";
                $this->view->name=$session->name;
             }
        }
    }
    
    
    class LoginController extends Zend_Controller_Action
    {
        public function loginaction(){
            $session = new Zend_Session_Namespace("login_session");
    
            if(isset($session->name)){
               //Redirect to New Page-Already Logged In
            } else {
               //Authenticate the user and if login is successful
               $session->name="USER_NAME";
            }
        }
    }
    

Thank You.

1 Answers1

1

This code looks ok except for the previously mentioned typo.

It's possible and likely that somewhere else in your code you inadvertently overwrite the session namespace. I think we've all done that at least once.

I would suggest that instead of trying to roll your own authentication solution, use the one that ZF provides: Zend_Auth

a basic Zend_Auth login/logout might look like:

//non production code for example only
public function loginAction()
    {
        $form = new Application_Form_Login();

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $data = $form->getValues();
                $authAdapter = new My_Auth_Adapter($data['username'], $data['password']);
                //authenticate
                $result = $authAdapter->authenticate();
                if ($result->isValid()) {
                    //store the user object
                    $auth = Zend_Auth::getInstance();
                    //access Zend_Auth session namespace
                    $storage = $auth->getStorage();
                    $storage->write($authAdapter->getUser());
                    //add message to flashmessenger
                    $this->message->addMessage('Welcome');
                    //redirect to the homepage
                    return $this->_redirect('/');
                } else {
                    //handle authentication errors
                    $this->view->loginMessage =
                        "Sorry, your username or password was incorrect";
                }
            } else {
                //handle form validation errors
                $this->_redirect('/users/index/register');
            }
        } else {
            //if not post render empty form
            $this->view->form = $form;
        }
    }

    public function logoutAction()
    {
        $authAdapter = Zend_Auth::getInstance();
        $authAdapter->clearIdentity();
    }

http://www.ens.ro/2012/03/20/zend-authentication-and-authorization-tutorial-with-zend_auth-and-zend_acl/

Good Luck!

RockyFord
  • 8,529
  • 1
  • 15
  • 21