重定向初始化 ci4 时无法设置登录记住我的 cookie
Unable to set login remember me cookies when redirect initialized ci4
我想在我的登录功能中添加记住我选项,当我设置记住我选项而不将用户重定向到他的访问成功页面时,cookie 存储完美,但问题是当我取消对重定向的注释时。当用户尝试登录时,它会重定向并绕过存储 cookie 的过程。
我该如何解决这个问题?
public function login(){
$username = $this->request->getPost('username');
$password = $this->request->getPost('password');
$user = $this->model->getLoginData($username);
$rowCount = count([$user]);
if($user){
if (password_verify($password, $user->password)){
//store session data
$this->auth->userSession($user);
//remember me
if(!empty($this->request->getPost("remember"))){
$cookie_hash = md5(uniqid()."sghsgd876mbjb");
set_cookie('hash_cookie', $cookie_hash, 36000 );
}else{
set_cookie('hash_cookie', '');
}
return redirect()->to('/manager');
}
}
}
您必须将 cookie 路径设置为 / 以适用于所有路径,而不仅仅是当前路径。
set_cookie('hash_cookie', $cookie_hash, 36000, '/' );
正如我读到的post
How can I set a cookie and then redirect in PHP?
您应该尝试使用 Codeigniter 助手而不是 set_cookie($cookie);
在这里你可以阅读更多 - LINK
尝试使用此功能发送 cookie:
// Copies all cookies from global response instance
return redirect()->back()->withCookies();
有关详细信息,请参阅文档 LINK
我想在我的登录功能中添加记住我选项,当我设置记住我选项而不将用户重定向到他的访问成功页面时,cookie 存储完美,但问题是当我取消对重定向的注释时。当用户尝试登录时,它会重定向并绕过存储 cookie 的过程。 我该如何解决这个问题?
public function login(){
$username = $this->request->getPost('username');
$password = $this->request->getPost('password');
$user = $this->model->getLoginData($username);
$rowCount = count([$user]);
if($user){
if (password_verify($password, $user->password)){
//store session data
$this->auth->userSession($user);
//remember me
if(!empty($this->request->getPost("remember"))){
$cookie_hash = md5(uniqid()."sghsgd876mbjb");
set_cookie('hash_cookie', $cookie_hash, 36000 );
}else{
set_cookie('hash_cookie', '');
}
return redirect()->to('/manager');
}
}
}
您必须将 cookie 路径设置为 / 以适用于所有路径,而不仅仅是当前路径。
set_cookie('hash_cookie', $cookie_hash, 36000, '/' );
正如我读到的post How can I set a cookie and then redirect in PHP?
您应该尝试使用 Codeigniter 助手而不是 set_cookie($cookie);
在这里你可以阅读更多 - LINK
尝试使用此功能发送 cookie:
// Copies all cookies from global response instance
return redirect()->back()->withCookies();
有关详细信息,请参阅文档 LINK