天天看點

zendframe session 存儲與判斷

<?php

class Custom_Controller_FrontendAction extends Zend_Controller_Action

{

   protected $customerSession;

    public function init()

    {

        //注冊session

       $this->view->customerSession = $this->customerSession = new Zend_Session_Namespace('customer');

    }

    public function loginAction()

    {

        if(isset($this->customerSession->customerId)){

            $this->_redirect('/');

        }

        if($this->getRequest()->isPost()){

            $post = $this->getRequest()->getPost();

            $email = strtolower(trim($post['email']));

            $password = sha1(md5(trim($post['password'])));

            try{

                if(empty($email)){

                    throw new Exception ('Please verify your email.');

                }

                $validatorEmail = new Zend_Validate_EmailAddress();

                if(!$validatorEmail->isValid($email)){

                    throw new Exception('Your email Address must be entered in this format: [email protected] Please try again.');

                }

                if(empty($post['password'])) {

                    throw new Exception('Please verify your password.');

                }

                $customerModel = new Model_Customer_Customer();                $db = $customerModel->getAdapter();                $authAdapter = new Zend_Auth_Adapter_DbTable($db);                $authAdapter->setTableName('customer')

                            ->setIdentityColumn('email')

                            ->setCredentialColumn('password');                $authAdapter->setIdentity($email)

                            ->setCredential($password);                $auth = Zend_Auth::getInstance();

                $result = $auth->authenticate($authAdapter);

                if($result->isValid()){

                    $customer = $authAdapter->getResultRowObject();

                    if('active' != $customer->status){

                        throw new Exception('Your account is inactive, please contact with the administrator.');

                    }

                    //登入成功修改customer值

                    $loginNowTime = date('Y-m-d h:i:s');

                    //擷取登入ip

                    Zend_Loader::loadClass('Custom_Plugins_Ip');

                    $loginIp = Custom_Plugins_Ip::getRealIpAddr();

                    $customerLoginArray = array('login_time' => $loginNowTime,

                                                'login_ip'   => $loginIp);

                    $customerModel = new Model_Customer_Customer();

                    $customerModel->updateById($customerLoginArray, $customer->id);                     

                    //儲存使用者Id到sessionID中

                    $this->customerSession->customerId = $customer->id;

                    $this->customerSession->email = $customer->email;

                    $redirectUrl = $this->getRequest()->getParam('redirect-url');

                    if(!empty($redirectUrl)){

                        $this->_redirect(urldecode($redirectUrl));

                    }else{

                        $this->_redirect('/');

                    }

                }else{

                    throw new Exception('The email or password is invalid.');

                }

            }catch(Exception $e){

                $this->view->error = $e->getMessage();

            }

        }

    }

    public function logoutAction()

    {

        $auth = Zend_Auth::getInstance();

        $auth->clearIdentity();

        $this->customerSession->unsetAll();

        $redirectUrl = $this->getRequest()->getParam('redirect-url');

        if(!empty($redirectUrl)){

            $this->_redirect(urldecode($redirectUrl));

        }else{

            $this->_redirect('/');

        }

    }