2012-01-30 62 views
6

Tôi muốn biết làm thế nào để sử dụng biến session trong PHP trong khuôn khổ zendlàm thế nào để sử dụng biến session trong PHP trong khuôn khổ zend

đây là mã của tôi cho đến nay: -

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; 

    } 

bây giờ tôi muốn để lưu trữ tên trong phiên và tôi muốn sử dụng ở một số trang khác như

echo "welcome," .$this->username; những gì tôi có thể làm?

Trả lời

6

Thay vì viết $identity thành $authStorage, bạn có thể lưu trữ một đối tượng tùy chỉnh hoặc một mô hình.

Dưới đây là một ví dụ:

<?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]); 
    } 
} 

Bây giờ trong điều khiển đăng nhập của bạn, bạn có thể làm:

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 

Thông thường, tôi có một đối tượng tài khoản mà tôi cũng lưu trữ trong đối tượng UserSession, và làm cho dễ dàng truy cập vào tên người dùng và userId thông qua các thuộc tính công khai.

Bây giờ bất cứ lúc nào bạn có thể nhận được các đối tượng:

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

Chỉ cần đừng quên để chắc chắn rằng nó là một Application_Model_UserSession.

+0

Tôi yêu câu trả lời của bạn, nhưng nó có thể là một overkill chút cho câu hỏi này. ;) – RockyFord

+0

@ drew010 :) Câu trả lời hay nhất – John

4

Mặc dù bạn có thể tạo đối tượng lớp Zend_Session, tuy nhiên tôi muốn giới thiệu đối tượng Zend_Session_Namespace thay thế. Bạn có thể khởi tạo phiên là:

 

$sess = new Zend_Session_Namespace('MyNamespace'); 
 

Nếu không vượt qua, Zend Session Namespace sẽ gán tên chuỗi là "mặc định". Để giá trị lưu trữ, bạn sẽ cần phải làm như sau:

 

$sess->username = 'you_name'; 
 

Sau đó trong mã của bạn, bạn sẽ cần phải làm như sau để lấy giá trị từ phiên:

 

$session = new Zend_Session_Namespace('MyNamespace'); 
$userName = $sess->username; 
 

Hy vọng nó giúp

4

Trong trường hợp này nên càng đơn giản càng tiếp tục thêm dữ liệu vào của bạn $ authStorage:

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

sau này trong một hành động hoặc điều khiển bạn có thể sử dụng Zend_Auth :: getStorage gọi lại dữ liệu của bạn hoặc sử dụng Zend_Session_Namespace.

$authStorage = $auth->getStorage(); 
    $authStorage->read($identity); 
    $authStorage->read($username); 

hoặc

$session = new Zend_Session_Namespace('Zend_Auth'); //Zend_Auth uses Zend_Session_Namespace for storage 
$username = $session->username; 
+0

Chỉ cần lưu ý, nếu bạn 'ghi' vào lưu trữ nhiều lần, nó sẽ ghi đè lên giá trị trước đó, để lưu trữ một đối tượng hoặc stdClass sẽ là cách để bạn có thể thay đổi thuộc tính của đối tượng. – drew010

+0

@ drew010 Tôi nhớ điều đó. Có vẻ như cách chính xác sẽ là lưu trữ mọi thứ trong bộ nhớ bằng cách sử dụng một lệnh viết.Tôi nghĩ rằng tôi sẽ phải thử nghiệm với điều này một số chi tiết. Cảm ơn vì tiền hỗ trợ. – RockyFord

Các vấn đề liên quan