没有mvc的Zend_auth。如何获取用户数据

时间:2012-06-19 11:03:46

标签: zend-framework zend-auth

我是zend的新手,我几乎了解它如何与mvc一起工作,但我试图在非mvc项目中只使用zend_auth。登录(auth)部分工作(即使我不确定它是否是最好的方法),但我无法弄清楚如何获取用户数据(在用户登录时存储)在另一个页。 这是我的login.php:

<?php
ini_set('display_errors', 1);
// define an absolute path to library directory
// you don't have to set a constant but it is just good practice 
// you can also use a variable or add the path directly below
define('APPLICATION_LIBRARY','c:/xampp/htdocs/website/');
// Note again: the path is the parent of your Zend folder, not the Zend folder itself.
// now set the include path
set_include_path(implode(PATH_SEPARATOR, array(
  APPLICATION_LIBRARY, get_include_path(),
)));
include("config.php");





        // i'm testing with a username and password that i know is inside database in the actual code is: $username= $_POST['username'] and the same for password.
         $username ='userx';
         $password = 'passx';
        $auth = Zend_Auth::getInstance();

         $adapter = new Zend_Auth_Adapter_DbTable($db);
         $adapter ->setTableName('profiles')
                  ->setIdentityColumn('username')
                  ->setCredentialColumn('password')
                  ->setIdentity($username)
                  ->setCredential($password);


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





            switch($result->getCode()) {
        case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
            $errorMessage = "Error: Name not correct.";

            // i'm sending username and password to another login that is exactly like this one, but searches in another table
            header("Location: login2.php?pass=".$password."&user=".$username);
            break;

        case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
            $errorMessage = "Error: pass and user are not valid";
            echo $errorMessage;

         header("Location: login2.php?pass=".$password."&user=".$username);
            break;

        case Zend_Auth_Result::SUCCESS:



            $adapter->getResultRowObject();
            $authStorage = $auth->getStorage(); // set storage. Session default

            $authStorage->write($result); // save user data


            if(isset($_POST['remember'])) {
                Zend_Session::rememberMe(60*60*24*7*4);
                $remember=$_POST['remember'];
                    }
            // send user to page they originally request, with username and pass because i don't know how to get them there from what i've stored before...i know it's not good.
       header("Location: profile_page.php?pass=".$password."&user=".$username."&remember=".$remember);
            break;

        default:
            $errorMessage = "Error.";
            break;
    }
?>

这是我的config.php文件:

<?php
error_reporting(E_ALL);

require_once('Zend/Session/Namespace.php');
require_once('Zend/Session.php');
require_once('Zend/Db/Table/Abstract.php');
require_once('Zend/Auth/Adapter/DbTable.php');
require_once('Zend/Auth.php');
require_once('Zend/Db/Expr.php');
require_once('Zend/Auth/Exception.php');
require_once('Zend/Controller/Action.php');


$params = array(
    'host'     => '127.0.0.1',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'website',
);

try {
    $db = Zend_Db::factory('Pdo_Mysql', $params);
    $db->getConnection();
} catch(Zend_Db_Adapter_Exception $e) {
    die("<pre>There was an error connecting to the server</pre>"); //Probably bad login or mysql daemon is not running.
} catch(Zend_Exception $e) {
    die("<pre>Something failed to load</pre>"); //factory() probably failed to load the adapter class
}

$users = new Zend_Session_Namespace('Zend_Auth');




?>

我想获取用户在其个人资料页面中进行身份验证时存储的数据。

1 个答案:

答案 0 :(得分:1)

你在存储中写错了而不是

        $authStorage->write($adapter->getResultRowObject();); // save user data