Php 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
zeeshan12
Forum Newbie
Posts: 3
Joined: Wed Nov 10, 2004 6:33 am

Php cookies

Post by zeeshan12 »

Hi,
I am having trouble managing cookies in php, could someone please have a look and sort this out or suggest a better way to do it.

I want to save an ID in the cookie on my site for 30 days. The Id comes in the URL when the user clicks the advert of my site displayed on an affiliate site. The problem is, If the user comes back on the site through a different affiliate and bring a different ID then the previous cookies should be deleted and a new cookie should be placed for 30 days again.

I can set the first cookie but i'm not able to delete it and place a new one. please see the code below

Code: Select all

//set a 30 days cookie. 
//if the user's comming from different affiliate then overwrite the existing cookie 

if(isset($_GETї'uid'])){ 
  if(!isset($_COOKIE&#1111;'aff_uid']) or $_COOKIE&#1111;'aff_uid']<>$_GET&#1111;'uid'])&#123; 
    $hostname = $_SERVER&#1111;'HTTP_HOST']; 
    setcookie("aff_uid", "", time()-3600,"/", "$hostname"); 
    $cookie_life = time() + 2592000; //30 days 
    setcookie("aff_uid", $_GET&#1111;'uid'], $cookie_life,"/", "$hostname"); 
  &#125; 
&#125;
when i display the cookie value on a different page it always shows me the old value.

Many Thanks,
Zeeshan. :(
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Try doing it without adding the hostname to the cookies.
zeeshan12
Forum Newbie
Posts: 3
Joined: Wed Nov 10, 2004 6:33 am

you are a star

Post by zeeshan12 »

Thanks kettle_drum ...i'ev removed the "hostname" from both setcookie() statments, and it works fine now..

I cannot thank you enough since i'ev been tryin to do this from yesterday..one more question...is my cookie secure now.?

Thanks
Zeeshan.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

By default it will only work on the domain that it is set on. As to being secure, its no more or less secure as it was to begin with as it doesnt have the last paramater set to make it secure, but thats only if your sending it over https.
zeeshan12
Forum Newbie
Posts: 3
Joined: Wed Nov 10, 2004 6:33 am

Post by zeeshan12 »

Thanks for the clearification....much appriciated.

zeeshan.
Post Reply