Welcome Guest, Not a member yet? Register   Sign In
CI4 Helper Function set_cookie doesn't create a cookie on browser
#1

(This post was last modified: 06-04-2021, 02:43 AM by vtankaria.)

CI4 Helper Function set_cookie doesn't create a cookie on browser

Hello Everyone,

I am working on Codeigniter  v4.1.2. I created a login page, and was trying to create a cookie using a Helper function set_cookie
.

Code:
set_cookie("cookie_name","cookie_value","cookie_expiry")

Inside Cookie Helper Code PROJECT_PATH/vendor/codeigniter4/framework/system/Helpers/cookie_helper.php

The set_cookie function calls the Service Response
Code:
setCookie 
Method.

In the Service Response setCookie Method, it calls the put method in PROJECT_PATH/vendor/codeigniter4/framework/system/Cookie/CookieStore.php

Code:
$this->cookieStore = $this->cookieStore->put($cookie);


Code:
public function put(Cookie $cookie)
{
    $store = clone $this;

    $store->cookies[$cookie->getId()] = $cookie;

    return $store;
}


The put method, just creates a clone of the class and assigns the sets the new cookie.
However, it doesn't create any cookie on browser and returns the class object.

I added the Line
Code:
$store->dispatch();
to create the cookie after its sent in the variable.
Code to Create a cookie on browser is written in dispatch() function.

Code:
public function put(Cookie $cookie)
{
$store = clone $this;

$store->cookies[$cookie->getId()] = $cookie;

$store->dispatch();// added by me, so a cookie is created on browser

return $store;
}


I think many people would be facing this issue. As I found similar issue posted on Stackoverflow https://stackoverflow.com/questions/6677...ot-setting , 2 months back.

Can someone help with this?

Thank you.
Reply
#2

Show your code. How do you set and read the cookie? 
Is it all on the same page?
Do you have the same problem as this? https://github.com/codeigniter4/CodeIgni...ssues/3939
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#3

Frirst call helper cookie
Helper('cookie' ) :
Then setcookies ()
Enlightenment  Is  Freedom
Reply
#4

I have good news for you. I understand your problem and I have the solution of your problem.
You are creating login for your application and after valid information you are trying to create cookie for logged in user.
After creating cookie you try to redirect the page, As I understand your problem. Here a short trick will do the work.
When you are redirecting instead of simply redirecting use below code:
PHP Code:
redirect()->to('url')->withCookies(); 

It will send cookie with redirect. Hence cookie will successfully generated in the browser.
Hope it solved your problem.
In CodeIgniter 4, I feel there should be more work on documentation. It's not completed yet. I get into this issue, and try many solutions but didn't get it right. After searching a lot, I just got the trigger. It should be mentioned in cookie helper with reference that when redirecting happens cookie should be sent along request. So that developer can understand how cookies work in CodeIgniter 4.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB