Zend 会话和 zend 身份验证

发布于 2024-12-29 06:33:03 字数 1389 浏览 1 评论 0原文

我已经通过 zend auth 创建了一个登录系统,这是代码

// userAuthentication
   public function authAction(){
       $request     = $this->getRequest();
       $registry    = Zend_Registry::getInstance();
       $auth        = Zend_Auth::getInstance(); 
       $DB = $registry['DB'];
           $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
               $authAdapter->setTableName('user')
                            ->setIdentityColumn('user_name')
                            ->setCredentialColumn('user_password');

      $username = $request->getParam('username');
      $password = $request->getParam('password');
      $authAdapter->setIdentity($username);
      $authAdapter->setCredential($password);
      $result = $auth->authenticate($authAdapter);

      if($result->isValid()){
           $data = $authAdapter->getResultRowObject(null,'password');
           $auth->getStorage()->write($data);
           $this->_redirect('/login/controlpannel');
       }else{
           $this->_redirect('/login/login');
        }
  }

,现在工作正常。用户(表)中有 user_id(列),其中也有用户名和密码。我需要从此表中获取特定的 user_id,只需登录并将其放入会话中,

$user_session = new Zend_Session_Namespace('user_session');
$user_session->username = $username;
$user_id->user_id       = $user_id;

以便我可以针对此 $user_id 查询一些信息并将结果传递到视图(名称)控制面板

I have made a login system through zend auth here is the code

// userAuthentication
   public function authAction(){
       $request     = $this->getRequest();
       $registry    = Zend_Registry::getInstance();
       $auth        = Zend_Auth::getInstance(); 
       $DB = $registry['DB'];
           $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
               $authAdapter->setTableName('user')
                            ->setIdentityColumn('user_name')
                            ->setCredentialColumn('user_password');

      $username = $request->getParam('username');
      $password = $request->getParam('password');
      $authAdapter->setIdentity($username);
      $authAdapter->setCredential($password);
      $result = $auth->authenticate($authAdapter);

      if($result->isValid()){
           $data = $authAdapter->getResultRowObject(null,'password');
           $auth->getStorage()->write($data);
           $this->_redirect('/login/controlpannel');
       }else{
           $this->_redirect('/login/login');
        }
  }

This work fine now. There is user_id (column) in user (table) where there are username and password too. I need to get that specific user_id from this table which just login and put it in session through

$user_session = new Zend_Session_Namespace('user_session');
$user_session->username = $username;
$user_id->user_id       = $user_id;

so that I can query some info against this $user_id and pass the result into view (name) controlpanel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光与爱终年不遇 2025-01-05 06:33:03

从存储中获取用户 ID:

$userInfo = Zend_Auth::getInstance()->getStorage()->read();

echo $userInfo->user_id;

Get user id from storage :

$userInfo = Zend_Auth::getInstance()->getStorage()->read();

echo $userInfo->user_id;
≈。彩虹 2025-01-05 06:33:03

虽然这个问题已经得到解答,但我倾向于比 getStorage()->read() 链更频繁地使用 getIdentity() 函数。下面的例子。

// to check if authenticated
Zend_Auth::getInstance()->hasIdentity();

// to actually get the details from storage
Zend_Auth::getInstance()->getIdentity()->user_id;

// if I need to use the identity over and over
$identity = Zend_Auth::getInstance()->getIdentity();
$userId = $identity->user_id;

While this was already answered, I tend to use the getIdentity() function more frequently than the getStorage()->read() chain. Examples below.

// to check if authenticated
Zend_Auth::getInstance()->hasIdentity();

// to actually get the details from storage
Zend_Auth::getInstance()->getIdentity()->user_id;

// if I need to use the identity over and over
$identity = Zend_Auth::getInstance()->getIdentity();
$userId = $identity->user_id;
呢古 2025-01-05 06:33:03

您可以按照 Teez 建议的方式访问数据,也可以从 Zend_Session_Namespace 中提取数据。

15.1.3.1。 PHP 会话中的默认持久性
默认情况下,Zend_Auth 提供成功的身份持久存储。
尝试使用 PHP 会话进行身份验证。成功后
身份验证尝试,Zend_Auth::authenticate() 存储身份
将认证结果放入持久存储中。除非
否则,Zend_Auth 使用名为的存储类
Zend_Auth_Storage_Session,它又使用 Zend_Session。定制
类可以通过提供实现的对象来使用
Zend_Auth_Storage_Interface 到 Zend_Auth::setStorage()。

Zend_Auth_Storage_Session 使用“Zend_Auth”会话命名空间。
可以通过将不同的值传递给
Zend_Auth_Storage_Session 的构造函数,该值在内部
传递给 Zend_Session_Namespace 的构造函数。这应该
发生在尝试身份验证之前,因为
Zend_Auth::authenticate() 执行自动存储
身份。

You can access the data the way Teez suggest or just pull it from Zend_Session_Namespace.

15.1.3.1. Default Persistence in the PHP Session
By default, Zend_Auth provides persistent storage of the identity from a successful
authentication attempt using the PHP session. Upon a successful
authentication attempt, Zend_Auth::authenticate() stores the identity
from the authentication result into persistent storage. Unless
configured otherwise, Zend_Auth uses a storage class named
Zend_Auth_Storage_Session, which, in turn, uses Zend_Session. A custom
class may instead be used by providing an object that implements
Zend_Auth_Storage_Interface to Zend_Auth::setStorage().

Zend_Auth_Storage_Session uses a session namespace of 'Zend_Auth'.
This namespace may be overridden by passing a different value to the
constructor of Zend_Auth_Storage_Session, and this value is internally
passed along to the constructor of Zend_Session_Namespace. This should
occur before authentication is attempted, since
Zend_Auth::authenticate() performs the automatic storage of the
identity.

随心而道 2025-01-05 06:33:03

将数组分配给会话时,必须为要创建的会话提供名称,即必须在执行 getStorage 之前执行 setStorage。

你必须这样编写你的代码:

   // userAuthentication
   public function authAction(){
       $request     = $this->getRequest();
       $registry    = Zend_Registry::getInstance();
       $auth        = Zend_Auth::getInstance(); 
       $DB = $registry['DB'];
           $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
               $authAdapter->setTableName('user')
                            ->setIdentityColumn('user_name')
                            ->setCredentialColumn('user_password');


      $username = $request->getParam('username');
      $password = $request->getParam('password');
      $authAdapter->setIdentity($username);
      $authAdapter->setCredential($password);
      $authAdapter->setStorage(new Zend_Auth_Storage_Session('User_Auth'));
      $result = $auth->authenticate($authAdapter);
      if($result->isValid()){
      $data = $authAdapter->getResultRowObject(null,'password');
       $auth->getStorage()->write($data);
       $this->_redirect('/login/controlpannel');
       }else{
           $this->_redirect('/login/login');
        }
  }

然后要获取你的存储值,你必须使用这个:

$x = new Zend_Auth_Storage_Session('User_Auth');
$y = $x->read();

并且你将 $y 中的所有内容作为对象获取。

享受!

assigning an array to a session, you must provide a name to the session you area creating, i.e. you must do setStorage before you do getStorage.

you must write your code like this:

   // userAuthentication
   public function authAction(){
       $request     = $this->getRequest();
       $registry    = Zend_Registry::getInstance();
       $auth        = Zend_Auth::getInstance(); 
       $DB = $registry['DB'];
           $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
               $authAdapter->setTableName('user')
                            ->setIdentityColumn('user_name')
                            ->setCredentialColumn('user_password');


      $username = $request->getParam('username');
      $password = $request->getParam('password');
      $authAdapter->setIdentity($username);
      $authAdapter->setCredential($password);
      $authAdapter->setStorage(new Zend_Auth_Storage_Session('User_Auth'));
      $result = $auth->authenticate($authAdapter);
      if($result->isValid()){
      $data = $authAdapter->getResultRowObject(null,'password');
       $auth->getStorage()->write($data);
       $this->_redirect('/login/controlpannel');
       }else{
           $this->_redirect('/login/login');
        }
  }

and then to get your storage value, you must use this:

$x = new Zend_Auth_Storage_Session('User_Auth');
$y = $x->read();

and you get everything in $y as an object.

Enjoy!

人间☆小暴躁 2025-01-05 06:33:03

这是我的方法,效果很好:
bootstrap /* 中定义 init 函数

protected function _initSession()
{

    $UserSession = new Zend_Session_Namespace('UserSession');
    $UserSession->setExpirationSeconds(/* you may fix a limit */);
    Zend_Registry::set('UserSession', $UserSession);
}

1-i 首先在登录操作中的 ,在正确的用户名和密码之后;密码*/

 // Create session
    $UserSession = Zend_Registry::get('UserSession');
 // Get the user from database 
 $db = Zend_Db_Table::getDefaultAdapter();
 $user = $db->fetchRow("SELECT * FROM user_table WHERE user_email = '".$user_email."'");

 //then you assign to $user to $UserSession variable : 
 $UserSession->user = $user;

 //finaly don't forget to unset session variable in the Logout action ...

This is my approach and it s working nice:
1-i start by defining an init function in the bootstrap

protected function _initSession()
{

    $UserSession = new Zend_Session_Namespace('UserSession');
    $UserSession->setExpirationSeconds(/* you may fix a limit */);
    Zend_Registry::set('UserSession', $UserSession);
}

/* in the Login action,after correct username & pwd */

 // Create session
    $UserSession = Zend_Registry::get('UserSession');
 // Get the user from database 
 $db = Zend_Db_Table::getDefaultAdapter();
 $user = $db->fetchRow("SELECT * FROM user_table WHERE user_email = '".$user_email."'");

 //then you assign to $user to $UserSession variable : 
 $UserSession->user = $user;

 //finaly don't forget to unset session variable in the Logout action ...
合约呢 2025-01-05 06:33:03

user = Zend_Auth::getInstance()->getIdentity();

if(!@$this->user){
$objSession->errorMsg = " Please Login First .. ! ";
$this->_redirect('/user/login');
}

?>

user = Zend_Auth::getInstance()->getIdentity();

if(!@$this->user){
$objSession->errorMsg = " Please Login First .. ! ";
$this->_redirect('/user/login');
}

?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文