我是 zend 的新手,我几乎理解它是如何与 mvc 一起工作的,但我试图在非 mvc 项目中只使用 zend_auth。login(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');
?>
我想获取用户在其个人资料页面中通过身份验证时存储的数据。