aboutcookies

Using Cookies on your site

Writing Cookies on  your site

We explain in easy to understand terms, how you can write cookies

More

Reading Cookies you have set

An easy to follow guide to reading cookies you have set

More

Deleting 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

More

aboutcookies © 2012 - Fortune Design by RichoSoft - All Rights Reserved | Terms of Use | Privacy Policy | Made by RichoSoft

Writing Cookies

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.

Javascript: In this example we will use javascript to write the cookie.

Place this script in the <head> section of your page:

Then you can use the following code to write a cookie:

You can use the code above to set a cookie when the page loads, when a user clicks a button, or within other javascripts.

Examples:

<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);” />

Inside other javascripts you would simply add the SetCookie('MyCookie','I have been here',1); within your other script.

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.

This is just a very short insight into setting a cookie, you can find lot’s more by searching for ‘Writing a Cookie with javascript” in an Internet search engine.

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)

Example:

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)


Cookies