Jump to content

PHP not reading javascript cookies


cleibesouza

Recommended Posts

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.

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

  • 3 years later...

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 by galloway
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.