douxiong3245
douxiong3245
2016-03-13 16:25

PHP获取在Web应用程序中设置的所有cookie

已采纳

Is there any way to get all cookies set in php application (with no request) ? I want to do something like this:

setcookie("cookieName", "test");
print_r($_COOKIE);

Above code of course doesn't work (work only when app is requested by browser)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dsfdsfds521521 dsfdsfds521521 5年前

    You can use headers_list() which will give you an array of headers that are ready to be sent to the client,

    headers_list(); //doesn't take any params

    eg result:

    Array ( [0] => X-Powered-By: PHP/5.5.9-1ubuntu4.9 [1] => Set-Cookie: cookieName=test )

    点赞 评论 复制链接分享
  • douyue4334 douyue4334 5年前

    Most of your question can be answered by just visiting the setcookie() php manual.

    Cookies cannot be verified until the next page load. You can manually set $_COOKIE['test'] at the time you call setcookie although this value will not be persistent if the user's browser does not store the cookie.

    Other than storing the values yourself, you can use headers_list() to return what headers will be, or have been, sent by PHP, but as stated earlier, this will not verify the cookie has actually been set on the user side.

    点赞 评论 复制链接分享