duancheng8000 2015-06-15 20:05
浏览 45
已采纳

PHP cookie,无法从cookie获取整数

I'm trying out PHP cookies for the first time. When I try to get the value from a string it works fine, but when I try to do that with an integer it prints this error:

Notice: Undefined offset: 1  

What does that mean?

My cookie code:

$CookieUserName = "demo";
$CookieUserID = 1;
setcookie($CookieUserName, $CookieUserID, time() + (86400 * 30), "/"); //     86400 = 1 day

Show the value of cookie

This works:

 echo $CookieUserName;

This doesn't:

echo $CookieUserID;
  • 写回答

1条回答 默认 最新

  • douqi5079 2015-06-15 21:20
    关注

    setcookie takes name & value as a string. You have your value set as int. Change your $CookieUserID to string. Below works fine for me.

    $CookieUserName = "demo";
    $CookieUserID = "1";
    setcookie($CookieUserName, $CookieUserID, time() + (86400 * 30), "/"); //     86400 = 1 day
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?