Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Cookies using Variable from URL

I'm no PHP expert and I'm trying to set a cookie that contains a referrer code from the URL. For example: www.example.com?promotioncode=google should set a cookie name promocode, value what ever is after the = and a 6 month expiry.

I can retrieve the promotioncode using

$_GET['promotioncode']

but I can't seem to insert this into the cookie string. I've tried a few ways:

$id = 'promo';
$value = $_GET['promotioncode'];
$time = time()+60*60*24*180;
setcookie($id, $value, $time);

and

$id = 'promo';
$time = time()+60*60*24*180;
setcookie($id, $_GET['promotioncode'], $time);

but it doesn't work. If I use a word or number as the cookie value then the cookie is set no problem.

What am I missing/doing wrong?

like image 530
Andy Avatar asked Jun 04 '11 15:06

Andy


1 Answers

Add a parameter to define the path on the server in which the cookie will be available on :

setcookie($id, $value, $time, '/');

It should work.

like image 80
SuN Avatar answered Oct 05 '22 23:10

SuN