PDA

View Full Version : How come my cookies are not being set?


ibeblunt
Sun 19th May '02, 12:52am
I've been working on this for 2 nights and I'm past frustrated. Basically, I'm trying to set a cookie and it's not being set. I even coded it the long way just to ensure that I wasn't forgetting any variables.

This is the code that I have at the top of the page BEFORE any headers are displayed:



if (!$cookie_750x400) {

$cookie_name = 'cookie_750x400';

$cookie_value = 'set';

$cookie_expire = time()+3600; // EXPIRES EVERY 60 MINUTES

$cookie_domain = '.sohh.com';

setcookie($cookie_name,$cookie_value,$cookie_expir e,'/',$cookie_domain,0);

$show_750x400_ad = 'yes';

}



I know in PHP if you set a cookie, it won't show up on the current page, which is why I have set the $show_750x400_ad to 'yes' for later usage.

Then I have this code:



if ($show_750x400_ad == 'yes') {

?>
<script language="JavaScript" type="text/javascript">
<!--
launch('http://www.sohh.com/mv-pop-up_750x400.html','750x400',750,400);
//-->
</script>
<?php

}



I made a page on my site, which shows me all the cookies stored on my computer from my domain and each time the cookie is not set.

What could I be doing wrong?

I am using PHP 4.1.2 and Internet Explorer 6.0.2600. I'm not too sure of what I have set in the php.ini file since it was installed for me.

Just so you can see the problem, I made this page:

http://www.sohh.com/test_setcookie.php

This page contains the code:



<?php

$cookie_name = "cookie_750x400";

$cookie_value = "set";

$cookie_expire = time()+3600;

$cookie_domain = ".sohh.com";

setcookie($cookie_name,$cookie_value,$cookie_expir e,"/",$cookie_domain,0);

?>



Then I made another page:

http://www.sohh.com/apps/cookies/

This will echo all the cookies that my domain is using on your computer.

What could I be doing wrong?

megahard
Sun 19th May '02, 7:45am
if (!isset($cookie_750x400)) {

scoutt
Sun 19th May '02, 3:04pm
you might have to try the superglobal. that is if you have teh globals turned off like it is recommended.

if (!isset($_COOKIE['cookie_750x400'])){

megahard
Sun 19th May '02, 3:23pm
Originally posted by scoutt
you might have to try the superglobal. that is if you have teh globals turned off like it is recommended.

if (!isset($_COOKIE['cookie_750x400'])){

nope he wont, cuz the script without the IF worked on his site.

its just he has a dodgy IF bit

DexterII
Wed 29th May '02, 8:27pm
Cause usually setting cookies has to be the first thing in the php file before it finishes sending the headers.

ibeblunt
Thu 30th May '02, 12:34pm
Originally posted by DexterII
Cause usually setting cookies has to be the first thing in the php file before it finishes sending the headers.

This is the code that I have at the top of the page BEFORE any headers are displayed:

However I have fixed the problem.

Scott MacVicar
Thu 30th May '02, 12:46pm
Cookies that are sent don't take effect until the next time the page is loaded, so the solution is to set the value after settings the cookie.

ibeblunt
Thu 30th May '02, 1:54pm
Originally posted by PPN
Cookies that are sent don't take effect until the next time the page is loaded, so the solution is to set the value after settings the cookie.

Was that not what this code at the top did:

if (!$cookie_750x400) {

$cookie_name = 'cookie_750x400';

$cookie_value = 'set';

$cookie_expire = time()+3600; // EXPIRES EVERY 60 MINUTES

$cookie_domain = '.sohh.com';

setcookie($cookie_name,$cookie_value,$cookie_expir e,'/',$cookie_domain,0);

$show_750x400_ad = 'yes';

}

But like I said in my last post. Problem solved.