我在用Yii做一個簡單的小blog,在做後台登陸驗證的時候,出現bug,
不提示登陸錯誤資訊,我做的是帳號,密碼,驗證碼不可為空。
而且也不與資料庫匹配。我已經測試過,資料庫連接完全沒有問題。
LoginController.php
/**
* 後台登陸控制器
*/
class LoginController extends Controller{
/**
* 後台登陸模板
*/
public function actionIndex(){
$loginForm = new LoginForm();
if(isset($_POST['$LoginForm'])){
$loginForm->attributes = $_POST['LoginForm'];
if($loginForm->validate()){
echo 1;die;
}
}
$this->render('index',array('loginForm' => $loginForm));
}
public function actions(){
return array(
'captcha' => array(
'class' => 'system.web.widgets.captcha.CCaptchaAction',
'height' => 25,
'width' => 80,
'minLength' => 4,
'maxLength' => 4,
'offset' => 3,
'padding' => 1
),
);
}
}
這個是model,User.php
/**
* 後台使用者模型
*/
class User extends CActiveRecord{
/**
* 必不可缺的方法一,返回模型
*/
public static function model($className = __CLASS__){
return parent::model($className);
}
/**
* 必不可卻的方法二,返回表名
*/
public function tableName(){
return "{{user}}";
}
}
這個是LoginForm.php中的一個方法
public function rules()
{
return array(
// username and password are required
array('username', 'required', 'message' => '使用者名稱不可為空'),
array('password', 'required', 'message' => '密碼不可為空'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
//自訂錯誤資訊
array('captcha', 'captcha', 'message' => '驗證碼錯誤'),
);
}
這個是輸出錯誤資訊的頁面 index.php
- error($loginForm,'username') ?>
- error($loginForm,'password') ?>
- error($loginForm,'captcha') ?>
這個是components目錄中的 UserIdentity.php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$userInfo = User::model()->find('username = :name', array(':name' => $this->username));
if($userInfo == NULL){
$this->errorCode = self::ERROR_USERNAME_INVALID;
return false;
}
if($userInfo->password !== md5($this->password)){
$this->errorCode = self::ERROR_PASSWORD_INVALID;
return false;
}
$this->errorCode = self::ERROR_NONE;
return true;
}
}
到底哪裡出錯了,資料庫連結已經測過,完全沒有問題。
現在的情況是,即使寫了錯誤資訊,也不提示,也不執行登陸驗證。除了驗證碼可以更換之外,完全就像一個靜態頁面。
回複討論(解決方案)
if(isset($_POST['$LoginForm'])){ ???
是 if(isset($_POST['LoginForm'])){ 吧
if(isset($_POST['$LoginForm'])){ ???
是 if(isset($_POST['LoginForm'])){ 吧
對對對,沒錯的。謝謝,謝謝...
我都不知道那個$是怎麼寫上去的...