问题
I'm working on a program dealing with cookies under the kohana's HMVC structure, and I find that Kohana has 3 ways to get/set the cookie. They are
Request::current()->cookie()
, Response->cookie()
, and the cookie class (Cookie::set(), get()
)
And PHP has a native setcookie()
function and $_COOKIE
to deal with cookies too.
Could anyone explain their differences and, what are the situations that they should be used respectively.
回答1:
Request::cookie()
prior to calling Request::execute()
on the same object is used to set the cookies that will be send (or have been sent in case of the initial request) along with the rest of the request.
Request::cookie()
during a Request::execute()
will replace $_COOKIE
.
Response::cookie()
during a Request::execute()
will replace setcookie()
.
Response::cookie()
after a Request::execute()
is used to get the cookies set back by the server.
The Cookie helper will sign your cookies and is used by HTTP_Header to set cookies set to the Response object in your initial Request object (see Response::send_headers()
in index.php
).
You probably do not want to use it yourself directly if you are trying to code HMVC safe.
来源:https://stackoverflow.com/questions/7543811/difference-between-the-kohanas-request-cookie-response-cookie-and-the-cook