kool_samule Posted June 14, 2010 Share Posted June 14, 2010 Hi Chaps, Quick question on this one, I've got a <noscript> tag in my HTML to check to see if Javascipt in enabled, but how do you check to see if cookies are enabled? Is there HTML code / PHP function, or do you simply try to set a cookie and redirect on failure to read the cookie you just to to set? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/ Share on other sites More sharing options...
rwwd Posted June 14, 2010 Share Posted June 14, 2010 Hi there Kool_samule, To check for this option, you would try to set a cookie, but place it in a if clause, as the function itself returns a bool therefore you can see if the function was set in the first place if(setcookie("cookieName", "cookieValue", time()*60*60*24), "/")){ //cookie was set }else{ //cookie was not set } Hope this helps you Cheers, Rw Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/#findComment-1072097 Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 That won't work. If you read the documentation on setcookie you'll see that the return value of the function does not specify if the client accepted the cookie or not. You can see one method of doing this in PHP here. Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/#findComment-1072105 Share on other sites More sharing options...
nblackwood Posted June 14, 2010 Share Posted June 14, 2010 this should work: <?php setcookie("check", time()+3600); if(!isset($_COOKIE['check'])) $message = 'Cookies must be enabled'; else $message = ''; ?> <html> <body> <?php echo $message;?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/#findComment-1072111 Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 No, that won't work either. Cookies are sent to the server upon every request. Because you set that cookie after the request was made the cookie won't populate the $_COOKIE super-global until the next request of the page. Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/#findComment-1072114 Share on other sites More sharing options...
nblackwood Posted June 14, 2010 Share Posted June 14, 2010 I did a couple of tests before I posted the code. It works just fine. Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/#findComment-1072129 Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 No it doesn't. Even if you have cookies enabled the first time you visit the page you will get a "cookies must be enabled" message. Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/#findComment-1072134 Share on other sites More sharing options...
kool_samule Posted June 15, 2010 Author Share Posted June 15, 2010 Ok, thanks chaps, even if there is a bit of confusion on how exactly to do this, is the method correct, i.e. Attempt to set a Cookie and then, say redirect on failure? Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/#findComment-1072303 Share on other sites More sharing options...
Alex Posted June 15, 2010 Share Posted June 15, 2010 No.. That won't work because there is no way to set a cookie and tell if it was successfully saved on the client's computer in the same request. Use the solution here: http://coderemix.com/php-cookies-enabled-check Quote Link to comment https://forums.phpfreaks.com/topic/204778-no-cookie-redirect/#findComment-1072446 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.