gpt4 book ai didi

php - ASK CakePHP - 使用密码字段

转载 作者:行者123 更新时间:2023-11-29 12:53:13 27 4
gpt4 key购买 nike

我在这里搜索了很多主题,但无法解决我的问题。请帮我检查一下。

我制作了注册页面,当我制作密码字段时......

我有users_controller.php,例如:

class UsersController extends AppController
{

var $name = "Users";
var $helpers = array('Paginator','Html');
var $paginate = array();

//Doi tuong component de thuc thi thao tac login
public $components = array
(
'Auth' => array
(
'authorize' => 'controller',
'loginRedirect' => array
(
'admin' => FALSE,
'controller' => 'users',
'action' => 'dashboard'
),
'loginError' => 'Invalid account',
'authError' => 'You don\'t have permission'
),
'Session'
);

//Ham loc cac user truoc khi truy cap trang
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('add');
$this->Auth->allow('viewuserall');
}

//Ham them moi user
public function add()
{
$this->layout = 'TDCake';

$this->User->set($this->data);
if($this->User->valid_user() == TRUE)
{
if(!empty($this->data))
{
$this->User->create();
if($this->User->save($this->data))
{
$this->Session->setFlash('User has been created!');
$this->redirect(array('action'=>'login'));
}
else
{
$this->Session->setFlash('Please correct the errors');
}
};
}
else
{
$this->Session->setFlash("Your data is NOT available");
}
}

//Ham login cho user
public function login()
{
$this->layout = 'TDCake';
if
(
!empty($this->data) &&
!empty($this->Auth->data['User']['username'])&&
!empty($this->Auth->data['User']['password'])
)
{
$user = $this->User->find
(
'first',array
(
'conditions'=>array
(
'User.email'=>$this->Auth->data['User']['username'],
'User.password'=>$this->Auth->data['User']['password']
),
'recursive' => -1
)
);
if(!empty($user) && $this->Auth->login($user))
{
if($this->Auth->autoRedirect)
{
$this->redirect($this->Auth->redirect());
}
}
else
{
$this->Session->setFlash
(
$this->Auth->loginError,
$this->Auth->flashElement,
array(),'auth'
);
}
}
}

//Ham logout cho user
public function logout()
{
$this->redirect($this->Auth->logout());
}

//Ham gi cha biet, de do tinh sau =))
public function dashboard()
{
$this->layout = 'TDCake';

}

//Ham view cac user khong dieu kien trong table users
function viewuserall()
{
$this->layout = 'TDCake';
$this->paginate=array
(
'limit' => 10,
'order' => array('id' => 'asc'),
);
$data = $this->paginate("User");
$this->set("data",$data);
}

}
模型中的

User.php 是:

class User extends AppModel
{
var $name = "User";
var $validate = array();

function validate_passwords()
{
if($this->data[$this->alias]['pass'] == $this->data[$this->alias]['rpass'])
{
return $this->data[$this->alias]['pass'] = $this->data['User']['password'];
}
else return FALSE;
}

function valid_user()
{
$this->validate = array
(
//Kiem tra username truoc khi add
'username' => array
(
'rule01_notEmpty' => array
(
'rule' => 'notEmpty',
'message' => 'You must enter your Username !'
),
'rule02_max16' => array
(
'rule' => array('maxLength', 20),
'message' => 'Your Username must be less than 20 chars !'
),
'rule03_exists' => array
(
'rule' => 'isUnique',
'message' => 'Your Username have already existed !'
)
),
//Kiem tra email truoc khi add
'email' => array
(
'rule01_notEmpty' => array
(
'rule' => 'notEmpty',
'message' => 'You must enter your Email !'
),
'rule02_exists' => array
(
'rule' => 'isUnique',
'message' => 'Your Email have already existed !'
),
'rule03_emailtype' => array
(
'rule' => 'email',
'message' => 'You didn\'t type a email !'
)
),
//Kiem tra password truoc khi add
'pass' => array
(
'length' => array
(
'rule' => array('between', 6, 20),
'message' => 'Your password must be between 8 and 40 characters.',
),
),
'rpass' => array
(
'length' => array
(
'rule' => array('between', 6, 20),
'message' => 'Your password must be between 8 and 40 characters.',
),
'compare' => array
(
'rule' => 'validate_passwords',
'message' => 'The passwords you entered do not match.',
)
)
);//End this->validate=array


if($this->validates($this->validate==TRUE))
{
return TRUE;
}
else
{
return FALSE;
}
}//End function valid_user

}

add.ctp

    echo $this->Session->flash('auth');
echo $this->Form->create();

echo $this->Form->input('username', array('label' => ('Username')));
echo $this->Form->input('email', array('label' => ('Email')));
echo $this->Form->input('pass', array('label' => ('Password'),'type' => 'password', 'value' => ''));
echo $this->Form->input('rpass', array('label' => ('Repeat Password'), 'type' => 'password', 'value' => ''));
echo $this->Form->input('firstname', array('label' =>('Firstname')));
echo $this->Form->input('lastname', array('label' =>('Lastname')));
echo $this->Form->input('dob', array('label' =>('DOB'),'type' => 'date'));


echo $this->Form->end('Register');

说明:因此,在这种情况下,我可以验证 2 个密码字段(空、不等于……),但它无法插入数据库。这意味着它将当前数据插入数据库,但数据库中的密码列为空。在数据库中,我的密码列名称也是“password”。

在另一种情况下,我将名称“pass”更改为“password”

echo $this->Form->input('pass', array(

当然,我已经更改了与...相关的任何地方

在这种情况下,它可以插入密码,但无法验证任何内容。

我对此太困惑了...我不知道我错在哪里...任何人都可以帮助我。

最佳答案

我不确定您为什么要在验证函数中进行分配:

   return $this->data[$this->alias]['pass'] = $this->data['User']['password'];

即使你正在做作业,它也应该是:

   return $this->data['User']['password'] = $this->data[$this->alias]['pass'];

意识到字段“password”正在从包含信息的 $this->data 获取值,而不是相反。

还有。将此代码分成两行会更好(就清晰度而言)。

$this->data['User']['password'] = $this->data[$this->alias]['pass'];
return $this->data['User']['password'];

如果数据库中的名称是“password”,则应将字段命名为“password”并且(如果未明确指定)。

您的添加函数没有执行上述操作,而且,作为最佳实践,您应该对密码进行哈希处理。

请参阅 CakePHP 书籍中的教程和示例。

花一些时间浏览一下所有的片段和建议。并且不要忘记标准。 :)

关于php - ASK CakePHP - 使用密码字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com