Welcome Guest, Not a member yet? Register   Sign In
How to check if cookies are enabled on a client
#1

[eluser]CIfan1000[/eluser]
Hi,

I am using CI's session class and I am happy with it.

However, it does not seem to generate an error if a user's cookies are not enabled.

I have written a function in a custom library that simply uses CI's get_cookie( ) to check for the cookie created by CI's session class, which I autoload.

I check for this cookie at the beginning of a controller and this works fine except if the page generated by the controller is the very first page the user loads. In this case the cookie has not been set by CI's session class as yet and so get_cookie fails.

This problem is solved if I check for a cookie after the user has pressed a submit button, but I wonder if this is the best way to go.

What is the best way to check if a client's cookies are enabled?

Any help would be appreciated.
Thanks!
#2

[eluser]Mike Ryan[/eluser]
Hi,

The only reliable way I've found to test this is to write a cookie, then read its value. Maybe you could incorporate this into a "check_cookies_enabled" function?
#3

[eluser]CIfan1000[/eluser]
Hi Mike,
Thanks for the post.

I have already written a function to read a cookie, one that is written by the CI system. This works fine.

This works except if the page generated by the controller is the very first page the user loads. In this case the cookie has not been set by CI’s session class as yet and so get_cookie fails.
#4

[eluser]Mike Ryan[/eluser]
You're welcome :-)

Unless I am misunderstanding something, this seems like normal behaviour. The user has never visited before, so the cookie doesn't exist. As long as get_cookie fails gracefully if no cookie exists, this shouldn't be causing any errors.




Theme © iAndrew 2016 - Forum software by © MyBB