cleibesouza Posted May 10, 2010 Share Posted May 10, 2010 Hi all, here's my problem. I need to check if javascript is enabled during a user visit. So, I'm trying to set a cookie using javascript then I try to read it using PHP. If cookie exists I'm setting a session variable that will be passed across the aplication. Keep in mind that I need to do all of this on the same page. I even tried to set the cookie (through javascript) on a different page and use a php header redirect but the php receiving page doesn't read the cookie. Here's the code: <script language="javascript"> function createCookie(name,value,days) { if(days){ var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); }else{ var expires = ""; }//end if/else document.cookie = name+"="+value+expires+"; path=/"; }//end function createCookie('isReady', 'y', 1); </script> PHP CODE: if(strlen($_COOKIE['isReady'])){ $_SESSION['javascriptReady'] = true; }//end if It only works when I refresh the page. Thanks for any help. Quote Link to comment Share on other sites More sharing options...
Sudantha Posted May 10, 2010 Share Posted May 10, 2010 may be it disstory the cookie and creates a new one for check is javascript is enabled or not why dont u use the <noscript></noscript> tags ? Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted May 10, 2010 Share Posted May 10, 2010 for check is javascript is enabled or not why dont u use the <noscript></noscript> tags ? This ^ It's built into HTML, theres no need for Javascript to do anything to check if they have it enabled. Quote Link to comment Share on other sites More sharing options...
cleibesouza Posted May 10, 2010 Author Share Posted May 10, 2010 I tried to follow your suggestion of the <noscript> tag and added this: <script language="javascript"> function createCookie(name,value,days) { if(days){ var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); }else{ var expires = ""; }//end if/else document.cookie = name+"="+value+expires+"; path=/"; }//end function createCookie('isReady', 'y', 1); </script> <noscript> <?php $_SESSION['javascriptReady'] = false; ?> </noscript> No success... Quote Link to comment Share on other sites More sharing options...
cleibesouza Posted May 10, 2010 Author Share Posted May 10, 2010 Thank you for your help. I found another solution Quote Link to comment Share on other sites More sharing options...
galloway Posted June 20, 2013 Share Posted June 20, 2013 (edited) I'm a little late here, and you have probably figured this out already, but the reason php only detects your javascript-set cookie after you refresh the page is that php has already process by the time you are looking at you web page, which is where and when javascript happens. So, by the time your javascript can set a cookie, it is too late for php to do anything until you reload the page. Server side processing like php, asp, cold fusion, perl, etc. always do their work before they serve you your html. Javascript operates only on what php has already processed. Edited June 20, 2013 by galloway Quote Link to comment 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.