php set cookie error and then reading cookie

F

-fool-

Guest
cookie.php is supposed to set a cookie called lolzz

Code:
<?php 
setcookie("lolzz", "", time()+36000);
?>
is the code there.

then on another page i have
Code:
<?php
if (isset($_COOKIE["lolzz"]))
header("Location: news.php");
else
echo "cookie not set"
?>
this redirects to the news.php if its set, or shows me that the cookie was set wrong (or not set at all).

right now the cookie never sets, and the redirect works even when the cookie is not set.
 

SD_Ryoko

Ultra Cool Member
Reaction score
85
Here is the full writeup on setcookie()
http://us2.php.net/setcookie

A few things.

The first might be your PHP configuration. It might not be setup to use globals that way. I had that problem on GRW. So try this instead

Code:
if (isset($HTTP_COOKIE_VARS['lolzz']))

Secondly, you arn't using a lot of fields when setting the cookie. Try using more fields.

Code:
setcookie("lolzz", value, expire, "/", ".yoursite.com");

Lastly, setting a cookie must be the FIRST thing on your page, before header information, web text or redirect locations.
 
F

-fool-

Guest
Code:
<?php 
setcookie("lolzz", "", time()+36000, "/", "foolz.us", "0");
?>

thats my new cookie code, name, value, time, directory, domain, boolean secure.

and my redirect code is

Code:
<?php
if (isset($HTTP_COOKIE_VARS['lolzz']))
header("Location: news.php");
else
echo "cookie not set"
?>

yet it still redirects with no cookie, and the cookie is still not set. could it be browser settings? the page that makes the cookie and the page that uses it are in the same directory.

BUT the page that sets the cookie opens in a new window (blank). is this the problem?
 

SD_Ryoko

Ultra Cool Member
Reaction score
85
First, I edited my post, so see the third note about when your allowed to set the cookie.

Then veryify it IS setting the cookie in your local folders.

Also, change "foolz.us" to ".foolz.us".

This allows access to the cookie throughout multiple subdomains.

The path I gave you allows acces through multiple directories.

Say you set the cookie in your root directory, fools.us.

Now when you try to access it in 'fools.us/thisdir' it won't exist for that folder.

Changing the location (.fools.us) allows site wide access to the cookie.
 
F

-fool-

Guest
ty. it all works now :)

now i can disable viewing the flash intro completely, instead of just hittng skip every time. so if you go to my site the first times, you can see the intro or click skip. and if you go a lot, you just get forwarded to the actual site.

awesome.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top