To set a cookie named <cookie name> to value <cookie value>, use set-cookie statement. All cookies must be set prior to sending out a header (see out-header
) or the program will error out and stop.
You can specify cookie's <expiration> date (as a a string, see get-time
) with "expires" clause (default is session cookie meaning the cookie expires when client session closes), its <path> with "path" clause (default is "/"), whether a cookie applies to the same site with "same-site" clause along with possible values of "Lax", "Strict" or "None", and whether a cookie is "HttpOnly" (meaning it's not accessible to client scripting) - that is the default setting which you can change with "no-http-only" clause.
Use "secure" if a secure connection (https) is used, in order to specify this cookie is available only with a secure connection.
Cookies are commonly used for session maintenance, tracking and other purposes. Use get-cookie
together with set-cookie to manage cookies.
To set a cookie named "my_cookie_name" to value "XYZ", that will go with the reply (back to the client, such as a browser) and expire in 1 year and 2 months from now, use:
get-time to define mytime year 1 month 2
set-cookie "my_cookie_name"=my_cookie_value expires mytime path "/" same-site "Lax"
set-cookie "my_cookie_name"=my_cookie_value no-http-only