Posts Tagged ‘cookies’

How to Delete Cookies?

Tuesday, July 29th, 2008

One thing you may have noticed if you have started writing Javascript to use cookies is that there is no actual delete command that can be used to delete a cookie after you have created it. This doesn’t mean that you can’t delete cookies, it just means that you need to understand how cookies work in order to be able to control when the system will delete them for you.

When you create a session cookie it will continue to exist for as long as the browser remains open and will be deleted as soon as the browser is closed. This is because session cookies are actually retained in memory by the browser and are never actually stored anywhere. You do not specify an expiry date when creating a session cookie.

If you want a cookie to last for a longer (or shorter) time than you get wioth a session cookie you need to create a first party cookie instead. With a first party cookie the cookie is actually stored in a file on your visitor’s hard drive. You specify an expiry date/time when creating a first party cookie that defines how long the cookie is to be retained on the hard drive. The cookie isn’t necessarily deleted when that date/time is reached but cookies that have passed their expiry date/’time are ignored and so as far as the browser is concerned they don’t exist.

So how does this help us if we decide that we need to delete a cookie befre the date/time that it is set to expire? Well the solution is quite simple, we change the expiry date of the cookie so that it will be considered to have already expired. Rather than having to remember what to do each time, let’s just create a small function for deleting whichever cookie that we want.

function del_cookie(name) {
document.cookie = name +
‘=; expires=Thu, 01-Jan-70 00:00:01 GMT;’;
}

Now all we need to do is to call this del_cookie() function passing it the name of whatever cookie it is that we wish to delete. The function will update the expiry date on the cookie to one long in the past so that the cookie will be considered to be expired and will be ignored by the browser exactly the same as if it didn’t exist.

Why choose that particular date for setting the expiry date to delete the cookie? Well it just happens that all of the date processing within Javascript sees the 1st January 1970 as its starting date and actually records all date/times internally as the number of milliseconds from midnight on that day. Using that date therefore is effectively equivalent to setting the expiry to zero which means that the cookie will be deleted even if your visitor has the date on their computer set incorrectly. Were we to choose a more recent date it would be possible (although very unlikely) that one of our visitors might have their computer date set incorrectly to one earlier than the exipry that we chose and so our attempt to delete the cookie wouldn’t then work for them.

Cookies vs Sessions

Tuesday, July 29th, 2008

The main difference between cookies and sessions is that cookies are stored in the user’s browser, and sessions are not. This difference determines what each is best used for.

A cookie can keep information in the user’s browser until deleted. If a person has a login and password, this can be set as a cookie in their browser so they do not have to re-login to your website every time they visit. You can store almost anything in a browser cookie. The trouble is that a user can block cookies or delete them at any time. If, for example, your website’s shopping cart utilized cookies, and a person had their browser set to block them, then they could not shop at your trouble .

Sessions are not reliant on the user allowing a cookie. They work instead like a token allowing access and passing information while the user has their browser open. The problem with sessions is that when you close your browser you also lose the session. So, if you had a site requiring a login, this couldn’t be saved as a session like it could as a cookie, and the user would be forced to re-login every time they visit.

You can of course get the best of both worlds! Once you know what each does, you can use a combination of cookies and sessions to make your site work exactly the way you want it to.

Firefox:Customized Security Settings

Friday, June 27th, 2008

Control the level of scrutiny you’d like Firefox to give a site and enter exceptions—sites that don’t need the third degree. Customize settings for passwords, cookies, loading images and installing add-ons for a fully empowered Web experience.