开发者

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?


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.


There are no differences between a simple $string and $_GET['key'], so the problem can't be this.

Also check your 4th arg of setcookie


try adding

if($_GET['promotioncode'])
    setcookie(.....)


$id = $_GET['promotioncode'];
setcookie('promotioncode', $id, time()+60*30*24*3600 , '/' );


Try this:

setcookie($id, "" . $value, $time);

The blank string ("") may help clarify or set the type of $value. The quotes basically make sure that the $value variable is passed in as a string parameter to the setcookie method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜