Auth 总结
时间:2010-03-31 来源:bing_fox
1. 简介
2. 最简单用法
<?php
class AuthController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
echo "Index";
/* Initialize action controller here */
}
public function loginAction(){
echo "Logine";
$username = "jack";
$password = "111111";
$authAdapter = new MyAuthAdapter($username, $password);
$result = $authAdapter->authenticate();
if($result->isValid()){
echo $result->getCode();
}
}
}
//////实现一个接口,来定义验证适配器
class MyAuthAdapter implements Zend_Auth_Adapter_Interface
{
/**
* Sets username and password for authentication
*
* @return void
*/ private $name;
private $pass;
//决定了验证需要的参数,一般是用户名和密码
public function __construct($username, $password)
{
$this->name=$username;
$this->pass=$password;
// ...
} /**
* Performs an authentication attempt
*
* @throws Zend_Auth_Adapter_Exception If authentication cannot
* be performed
* @return Zend_Auth_Result
*/
//定义验证方式,一般是字符比照
public function authenticate()
{
echo "Auth:".$this->name;
echo "Auth:".$this->pass;
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, NULL, array("OK"));
}
}
?>
///可能的code
Zend_Auth_Result::SUCCESS
Zend_Auth_Result::FAILURE
Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND
Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID
Zend_Auth_Result::FAILURE_UNCATEGORIZED
/// 3. 数据库表认证
public function authenticate()
{
$select=$this->db->select();
$select->from("student","*");
$stmt = $this->db->query($select);
$decry = md5($this->pass);
$row = $stmt->fetch();
echo $row['name']."-=-=-=-=-=-".$row['password']."<br>";
if($this->name == $row['name'] && $decry == $row['password']){
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, NULL, array("OK"));
}
else{
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_UNCATEGORIZED, NULL, array("NO"));
}
}
4. 随机验证码(字母)
$dynamicSalt = "";
for ($i = 0; $i < 5; $i++)
{
$dynamicSalt .= chr(rand(65, 90));
}
echo $dynamicSalt;
2. 最简单用法
<?php
class AuthController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
echo "Index";
/* Initialize action controller here */
}
public function loginAction(){
echo "Logine";
$username = "jack";
$password = "111111";
$authAdapter = new MyAuthAdapter($username, $password);
$result = $authAdapter->authenticate();
if($result->isValid()){
echo $result->getCode();
}
}
}
//////实现一个接口,来定义验证适配器
class MyAuthAdapter implements Zend_Auth_Adapter_Interface
{
/**
* Sets username and password for authentication
*
* @return void
*/ private $name;
private $pass;
//决定了验证需要的参数,一般是用户名和密码
public function __construct($username, $password)
{
$this->name=$username;
$this->pass=$password;
// ...
} /**
* Performs an authentication attempt
*
* @throws Zend_Auth_Adapter_Exception If authentication cannot
* be performed
* @return Zend_Auth_Result
*/
//定义验证方式,一般是字符比照
public function authenticate()
{
echo "Auth:".$this->name;
echo "Auth:".$this->pass;
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, NULL, array("OK"));
}
}
?>
///可能的code
Zend_Auth_Result::SUCCESS
Zend_Auth_Result::FAILURE
Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND
Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID
Zend_Auth_Result::FAILURE_UNCATEGORIZED
/// 3. 数据库表认证
public function authenticate()
{
$select=$this->db->select();
$select->from("student","*");
$stmt = $this->db->query($select);
$decry = md5($this->pass);
$row = $stmt->fetch();
echo $row['name']."-=-=-=-=-=-".$row['password']."<br>";
if($this->name == $row['name'] && $decry == $row['password']){
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, NULL, array("OK"));
}
else{
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_UNCATEGORIZED, NULL, array("NO"));
}
}
4. 随机验证码(字母)
$dynamicSalt = "";
for ($i = 0; $i < 5; $i++)
{
$dynamicSalt .= chr(rand(65, 90));
}
echo $dynamicSalt;
相关阅读 更多 +