Page 1 of 1

Php cookies

Posted: Wed Nov 10, 2004 6:36 am
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. :(

Posted: Wed Nov 10, 2004 6:54 am
by kettle_drum
Try doing it without adding the hostname to the cookies.

you are a star

Posted: Wed Nov 10, 2004 7:00 am
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.

Posted: Wed Nov 10, 2004 7:34 am
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.

Posted: Wed Nov 10, 2004 7:48 am
by zeeshan12
Thanks for the clearification....much appriciated.

zeeshan.