
Adds a cookie to the current Response headers.
Cookies are used to store small pieces of persistent information for the server to remember users by. Typically these values should be kept very small and hold only key values to look up additional information about a user. Cookie space is limited in browsers and all applications running on a given site share the cookie space, so don't abuse use of it.
For more info on cookies and values you can look at the MDN topic on Set-Cookie.
Setting Simple Cookies without Options
foxpro
Response.AddCookie("cookieName","CookieValue")
There are additional parameters that allow you set expiration and cookie options, but if you need to set extra optoins we recommend you use the more comprehensive wwCookie parameter to set the options you need.
Using wwCookie to set complex cookies
The recommended way to add a cookie is:
foxpro
loCookie = CREATEOBJECT("wwCookie")
loCookie.CookieName = "testvalue"
loCookie.Value = "NewValue"
loCookie.Expires = DATE() + 10
loCookie.SameSite = "Strict" && Strict / Lax or "" for 'default' which is `None`
loCookie.Secure = .T. && Https only!
loCookie.HttpOnly = .T. && Http only can't be read in script code - defaults to .T.
* lcCookieVal = loCookie.ToString() && returns the generated cookie text
Response.AddCookie(loCookie) && add the Cookie to the current Response
Note that many of the properties are optional - only CookieName
and Value
are required. For more information on the values to set see that wwCookie class.
You can also pass explicit parameters to the method that map the object properties. The cookie is eventually stored as an object in the Response.Cookies collection.
o.AddCookie(toCookie | tcCookie,tcValue, tcPath,tcExpire, tlHttpOnly, tlSecure)
Parameters
toCookie
An instance of a wwCookie object with its values set.
tcCookie
The name of the Cookie to set.
tcValue
The string value of the cookie
tcPath
Optional - The Web server path to set the cookie on on. Default: /
Note it's best to leave this set for the root folder (/) as there are problems in older browsers respecting pathed cookies properly.
ttExpires
Optional - Specify the cookie expiration. Preferrably specify a date or for a session cookie but several formats are available:
-
* DateTime value of absolute expiration date/time
* Number of seconds from now
* `"NEVER"` (expires in 1 year)
* MimeDate format string: Sun, 28-Dec-2015 01:01:01 GMT (not recommended)
If passed as empty (.F.
or ""
or { : }
) the cookie expires when the browser is shut down, which is default browser behavior.
tcDomain
Optional domain name if you want to be explicit. You can also specifify .mydomain.com to set a cookie for all subdomains (ie. mydomain.com,www.mydomain.com,store.mydomain.com) etc. If not specified the domain of the current request is applied by the browser.
tlHttpOnly
Sets the HttpOnly flag on a cookie. HttpOnly cookies cannot be accessed in client script and thus mitigate the risk of Cross Site Script attacks against cookie access from script. default: .T.
tlSecure
Sets the Secure flag on a cookie. default: .F.
See also:
Class wwPageResponse© West Wind Technologies, 1996-2025 • Updated: 02/21/25
Comment or report problem with topic