Welcome Guest, Not a member yet? Register   Sign In
has_cookie helper always return false?
#1

I have done set_cookie() on another method,
but I want to check with has_cookie() in another method,
but always returns false.

Whereas if run on the same method, set_cookie()
then has_cookie(), the result is as expected.

the question is, how can has_cookie() be executed without the previous set_cookie()?
Reply
#2

Since you are using already the has_cookie() function, I'm assuming you are using the development version of CI4 because this helper is not available in v4.1.1 and below.

I don't know your use case for using has_cookie() but be mindful that this function checks if a Cookie object is still in the current Response object. When you call set_cookie() in a method then calls has_cookie() in another method, the cookies are already dispatched. By dispatch I mean the cookies are already registered with PHP. After dispatch, the Response clears out this collection of cookies. So you should know by now that has_cookie will check your cookie name against an empty collection, thus returning false.

If your use case rather is to check if the cookie you set in one method is registered into PHP via the $_COOKIE array, then you need to use the get_cookie() function instead. This verifies the cookie against the Request class. This returns null if the cookie is not found in the $_COOKIE array.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB