- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个标准的登录表单,如下所示:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
<?php $this->endWidget(); ?>
我的 UserIdentity 类是:
public function authenticate()
{
// $username = $this->username;
// $password = $this->password;
$user = Users::model()->findbyAttributes(array($username=>$this->username));
if($user === NULL){
$this->errorCode=self::ERROR_UNKNOWN_IDENTITY;
}else if ($user->password !== md5($this->password)){
// $this->username = $user->username;
// sess('SESS_USER_INFO', $user->attributes);
// $this->errorCode=self::ERROR_NONE;
//invalid password
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else {
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
}
我的 UserController.php actionLogin 如下:
public function actionLogin()
{
$model= new Users();
// Yii::app()->sessiion[username]= Yii::app()->model($username);
// Yii:app()->session[password] = Yii::app()->model($password);
// if it is ajax validation request
if(isset($_POST['ajax']))
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// print_r($_POST['Users']);
// collect user input data
$form = new LoginForm;
// if(isset($_POST['Users']))
// {
// $model->attributes=$_POST['Users'];
// validate user input and redirect to the previous page if valid
// if($model->login())
// $this->redirect(Yii::app()->user->getReturnUrl('index'));
// }
// display the login form
// $this->render('login',array('model'=>$model));
if(isset($_POST['LoginForm'])){
$form->attributes=$_POST['LoginForm'];
if($form->validate() && $form->login()) $this->redirect(Yii::app()->user->returnUrl);
}
$this->render('login',array('model'=>$model));
}
LoginForm模型如下:
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'rememberMe'=>'Remember me next time',
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
// $this->_identity=new UserIdentity($this->username,$this->password);
// if(!$this->_identity->authenticate())
// $this->addError('password','Incorrect username or password.');
$identity = new UserIdentity($this->username, $this->password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
$duration=$this->rememberMe ? 3600*24*30 : 0; //30 days
Yii::app()->user->login($identity,$duration);
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('username', 'Username is incorrect.');
break;
default; // UserIdentity:ERROR_USERNAME_INVALID
$this->addError('password','Password is incorrect.');
break;
}
}
}
/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
}
我们遇到的问题是,当我们在主页上使用有效凭据登录时,会加载一个新的登录表单,但不会指示用户已通过身份验证。我们还看到语法错误,指出登录表单中的变量 $model 未定义。
更新:我们不使用/views/site/login ,而是想使用/views/users/login 并将其连接到我们的 UserController。
如何解决上述问题?
最佳答案
首先,我注意到您将 $model
作为 User
Object
的类型传递,而不是 LoginForm Object
。首先改变那个。其次,因为 user
作为 model
发送(不确定你的 User 模型中有什么)
isset($_POST['LoginForm'])
将始终为 false
并会再次转到登录表单
关于php - Yii 框架 undefined variable $model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11744412/
目前我的后端和前端都有 2 个 Yii 安装设置。但是在处理这个问题时,它会变得有点困惑,我想知道这是否以正确的方式完成。下面是如何设置我的文件夹结构的示例。 - backend - assets
如何禁用通知,我在 idex.php 中尝试但通知是回声,我如何禁用它。 在 php.ini 最佳答案 更新 public/index.php
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
'urlManager'=>array( 'urlFormat'=>'path', 'rules'=>array( '' => array('site/page/vie
从 Yii 到 Yii 2.0 这行代码怎么写: Yii::app()->getRequest()->getRestParams() ? 最佳答案 在我看来,相当于 Yii::app()->getRe
我想使用多个调用构建查询,但在使用此代码时出现错误 $command = Yii::app()->db->createCommand() ->select('*') ->from('{
Yii中我的模型规则函数中的以下代码 public function rules() { // NOTE: you should only define rules for those att
我正在开发一个带有主数据库和多个数据库的系统,每个客户端一次。当客户填写并提交包含所有必需详细信息的表单时,将创建客户数据库及其表。 我的问题:Yii 框架是否支持动态创建数据库和表?如果是这样,是否
我知道我可以在 Yii 中注册一个新的元标记并且我知道怎么做,但是我需要 替换我设置的默认标签,因为当我在一篇文章时,我想插入 元标记中文章的简短描述; 如何管理元标记? 最佳答案 如果您使用的是最新
我尝试仅在管理模块中加载 Yii Bootstrap 扩展,但它不起作用。我假设我需要预加载它或以某种方式启动它......谢谢! class AdminModule extends CWeb
我已经建立了新的 Yii2 项目。现在我想 reorganize folder structure在“public”和“app”两个文件夹中(实际上代表 protected 文件)。 框架中的所有代码
页面 Controller 访问规则: public function accessRules() { $isadmin = User::loadUser(
代理人: agent_id (primary key) 用户: f_id (foreign key) type 我以这种方式创建了关系 public function relations() {
大家好,感谢阅读。 我想知道如何将数字格式化为货币,或者只是在末尾附加 €。我在 yii 框架的管理页面上的 gridview 中工作。 我有这个,例如 'columns'=>array(
如何禁用 Yii 的内置身份验证? (/site/login ). 我正在使用扩展程序进行身份验证并且不想让内置登录保持打开状态 - 这可能是一个安全问题。 最佳答案 我认为您可以删除站点 Contr
使用 Yii,我想删除所有不是今天的行。 我的解决方案好吗? $query = "delete from `user_login_hash` where `day`!='".(date('Y-m-d'
gridview yii如何在一列数组值中显示两个关系值 在我的模型代码中有关系 $criteria->compare('exp.ExperienceYear',$this->Experience,
是他们对的任何常用方法吗?为我的 yii 项目添加扩展 ? 如何向我的 yii 添加扩展名 请解释 步骤 最佳答案 “ 应用程序配置 ”在安装扩展时起着重要作用。默认情况下,此配置将位于 php 文件
如何在 yii 中使用场景禁用文本字段?我有 3 类帐户 super 管理员、管理员和普通用户。所有 3 类用户都有权更新有关他们的信息,但其中一个字段 accountId 只能由 super 管理员
我在 yii 中找不到太多关于将默认范围应用于模型的文档,我想知道是否有人可以解释或指出我正确的方向。 我的问题的快速版本: 是否可以向默认范围添加关系,或者默认情况下向模型上的每个 AR 搜索添加“
我是一名优秀的程序员,十分优秀!