CI4 Helper Function set_cookie doesn't create a cookie on browser |
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 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) 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(); Code to Create a cookie on browser is written in dispatch() function. Code: public function put(Cookie $cookie) 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.
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
/*** NO support in private message - Use the forum! ***/
Frirst call helper cookie
Helper('cookie' ) : Then setcookies ()
Enlightenment Is Freedom
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. |
Welcome Guest, Not a member yet? Register Sign In |