Having major difficulty with cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

Having major difficulty with cookies

Post by jaymoore_299 »

Depending on whether I run my script from domain.com/script.php or http://www.domain.com/script.php , I get two different cookies for each address. The problem is, I may have the ability to create two different cookies, but I only have one way to read the cookies, with the php code:
$cookievalue = $_COOKIE['visitor_']

This particular code only seems to look for domain without the www at the beginning, and so most visitors will not have this cookie unless they come to my site with "domain.com/.. "

I have tried this in IE and firefox and I get the same result. If I visit the page like this domain.com , then I get a user@domain.com cookie only, but when I visit http://www.domain.com I get only a user@www.domain.com cookie.
But $cookievalue = $_COOKIE['visitor_'] will only read the cookie without the www.

Is there some php setting I need to change to fix this? Basically I need to either set a cookie for both domains at once, or be able to read either cookies specifically. Any ideas?
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

its when you set the cookie, you need the . infront of the domain like this

Code: Select all

setcookie('visitor_', $value, time()+3600, '/', '.domain.com');
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

Post by jaymoore_299 »

I tried doing this.

on the main page I have this

Code: Select all

if (!empty($_COOKIE['visitor_'])) {
setcookie ("visitor_", "", time()-60000); 
setcookie ('visitor_', $url, time() + (60*60*24),'/','.domain.net');
}

// Otherwise he is unique (new or hasn't visited in 24 hours)
else{
setcookie ('visitor_', $url, time() + (60*60*24),'/','.domain.net');
$unique_=1;
}
every time he clicks on a link he is sent to a script that has this code in it

Code: Select all

$referer_url = $_COOKIE['visitor_'];
$referer_url always seems to come up empty
Post Reply