Sets a cookie by name to the specified string value.

It takes two parameters:

  1. The cookie name.
    This can be blank. It should not include a "=", or the part before the first "=" will become the real cookie name.
  2. The cookie value.
    The cookie value can be blank. Setting a cookie to blank will not necessarily delete it from the browser's cache, so the browser may send it with future requests, just with a blank value.


Cookies are useful for storing long-term session data, as browsers will send the cookie with future requests to the same hostname, so you can use something unique to keep the clients identifiable, keep them logged in, etc.

These are only cleared when the cookie expires, or when the browser clears their stored cookie data.


This action will result in a response Set-Cookie header like:

Set-Cookie: cookiename=cookievalue


Future requests from the browser will then include a Cookie header:

Cookie: cookiename=cookievalue; cookiename2=cookievalue2


Note there is a lot of confusion about what characters are allowed and not allowed. You can read more about it here.