PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 7, 2022

[FIXED] Unable to set login remember me cookies when redirect initialized ci4

 February 07, 2022     codeigniter, codeigniter-4, php     No comments   

Issue

I want to add remember me option to my login functionality, When I set remember me option without redirecting user to his access success page, the cookie stored perfect, but the issue is when I uncomment the redirection. When user attempt to login it redirect and bypass the process to store cookies. How can I fix this issue?

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');               
        }
    }
}

Solution

You must set cookie path to / to work for all paths, not just current one.

set_cookie('hash_cookie', $cookie_hash, 36000, '/' );

As I read on this post How can I set a cookie and then redirect in PHP?



Answered By - Jonathan A
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing