We explain in easy to understand terms, how you can write cookies
An easy to follow guide to reading cookies you have set
Cookies cannot be deleted directly from a web page, you have to ‘expire’ them so that the browser can delete them for you
Here we will give a basic overview on how to write a cookie from a page on your site, that you can use later by recalling the information it contains.
Place this script in the <head> section of your page:
Then you can use the following code to write a cookie:
<body onload=”SetCookie('MyCookie','I have been here',1);”> would set a cookie called ‘MyCookie’ with a value of ‘I have been here’ that expires in 24hrs time (1 day) when the page loads.
You could also add an onclick event to a button, image etc. like:
<Img src=”myphoto.jpg” onclick=”SetCookie('MyPhotoCookie','Seen myphoto',1);” />
The first parameter in the SetCookie('MyCookie','I have been here',1); is the Name of the Cookie and you will need to know this to read the cookie.
The second parameter is the Value of the cookie. (The text you want to use somewhere later.)
The third parameter is the expiry of the cookie in days which is converted to a date and time in the script we placed in the header. So a value of 1 would mean the browser will delete the cookie in 24 hours after it is set, a value of 365 would mean it expires (and gets deleted) in 1 year from when it was set.
You do have the option to set the path of the cookie. Setting the path to a folder in your site, restricts access to the cookie to pages it that folder only. Setting the path to the root “/” as we have here, means the cookie can be accessed by all pages in your site.
If you set a specific path, and then try to delete (expire) or read the cookie from another folder, you will not be able to.
You can also write cookies with ASP and PHP scripts, you do have some extra options with these, for example you can write Keys, which can save and read more than one value using Name Value pairs. (PHP scripts not covered here at present)
You want to save the user name and password in a single cookie:
In ASP to write a cookie called “UserInfo” you would use something like:
Response.Cookies(“UserInfo”)(“UserName”)=”Freddie1” (The UserName is the Name of the Key, Freddie1 the value)
Response.Cookies(“UserInfo”)(“PassWord”)=”MyPass1” (The PassWord is the Name of the Key, MyPass1 the value)
Response.Cookies(“UserInfo”).Expires = Date() + 7 (The Cookie UserInfo Expires in 7 days from now)
|Writing Cookies on Your Site|
|Reading Cookies on Your Site|
|Deleting Cookies on Your Site|