How to Delete Cookies?

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.

What is Cookie?

July 29th, 2008

The most common meaning of “Cookie” on the Internet refers to a piece of information sent by a Web Server to a Web Browser that the Browser software is expected to save and to send back to the Server whenever the browser makes additional requests from the Server. Depending on the type of Cookie used, and the Browsers’ settings, the Browser may accept or not accept the Cookie, and may save the Cookie for either a short time or a long time. Cookies might contain information such as login or registration information, online “shopping cart” information, user preferences, etc. When a Server receives a request from a Browser that includes a Cookie, the Server is able to use the information stored in the Cookie. For example, the Server might customize what is sent back to the user, or keep a log of particular users’ requests. Cookies are usually set to expire after a predetermined amount of time and are usually saved in memory until the Browser software is closed down, at which time they may be saved to disk if their “expire time” has not been reached. Cookies do not read your hard drive and send your life story to the CIA, but they can be used to gather more information about a user than would be possible without them. From Matisse

Cookies vs Sessions

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.

Running PHP and MySQL

July 24th, 2008

In order to do some of our tutorials (or program with PHP and MySQL in general) you need to be able to run PHP and MySQL. Although most free hosts do not have PHP and MySQL support there are some that do, and there are also many low cost hosting options available. If you already have hosting and are unsure if you have PHP and MySQL support you should contact your host directly. If you are shopping for hosting be sure that they support PHP and that your package includes at least one MySQL database.

Another option is to install PHP and MySQL directly onto your Windows computer. If you are a Mac user you already have the capability and just need to activate PHP and MySQL.

How do PHP and MySQL work together?

July 24th, 2008

PHP and MySQL compliment each other to do with neither can do alone. PHP can collect data, and MySQL can in turn store the information. PHP can create dynamic calculations, and MySQL can provide it with the variables it uses. PHP can create a shopping cart for your web store, but MySQL can then keep the data in a format PHP can use to create receipts on demand, show current order status, or even suggest other related products.

Although PHP and MySQL can each be used independently, when you put them together it opens up countless possibilities for your site. As the internet progresses, it becomes more and more necessary to deliver dynamic content to keep up with the demands of web surfers and their desire to have information instantly delivered to them online. By learning to use PHP and MySQL you can deliver this information to them on demand.

What is MySQL?

July 24th, 2008

MySQL is a relational database system that is used to store information. MySQL can store many types of data from something as tiny as a single character to as large as complete files or graphics. Although it can be accessed by most programing languages, it is often coupled with PHP because they work together with ease.

Information stored in a MySQL database hosted on a web server can be accessed from anywhere in the world with a computer. This makes it a good way to store information that needs the ability to change over time, but also needs to be accessed over the net. Some examples that can utilize MySQL are a web message board or a customer’s shipping status.

What is PHP?

July 24th, 2008

PHP is a scripting language that is often imbedded into HTML to add functions HTML alone can’t do. PHP allows you to collect, process and utilize data to create a desired output. In short, it let’s you interact with your pages.

PHP is able to preform a number of tasks including printing data, making numeric calculations (such as addition or multiplication), making comparisons (which is bigger, are they equal, etc) and making simple boolean choices. From this you can create more complex loops and functions to make your page generate more specialized data.

Why use PHP and MySQL?

July 24th, 2008

PHP and MySQL combine to be an easy yet powerful way to create dynamic web pages that actually interact with your visitors. HTML can create useful and well formatted web pages. With the addition of PHP and MySQL you can collect data from your users, create specific content on the fly, and do many other things that HTML alone can’t do.

The beauty of PHP as a language is that it is designed to be used along with HTML. You can use PHP right inside your already existing HTML content, or put HTML tags right inside your PHP coding. When learning PHP you are not making your existing HTML knowledge obsolete, you are instead adding to it to give it more functions and abilities.

MySQL-Definition

July 24th, 2008

MySQL is a relational database management system (RDBMS) based on SQL (Structured Query Language). First released in January, 1998, MySQL is now one component of parent company MySQL AB’s product line of database servers and development tools.

Many Internet startups became interested in the original open source version of MySQL as an alternative to the proprietary database systems from Oracle, IBM, and Informix. MySQL is currently available under two different licensing agreements: free of charge, under the GNU General Public License (GPL) open source system or through subscription to MySQL Network for business applications.

MySQL runs on virtually all platforms, including Linux, Unix, and Windows. It is fully multi-threaded using kernel threads, and provides application program interfaces (APIs) for many programming languages, including C, C++, Eiffel, Java, Perl, PHP, Python, and Tcl.

MySQL is used in a wide range of applications, including data warehousing, e-commerce, Web databases, logging applications and distributed applications. It is also increasingly embedded in third-party software and other technologies. According to MySQL AB, their flagship product has over six million active MySQL installations worldwide. Customers include Cisco, Dun & Bradstreet, Google, NASA, Lufthansa, Hyperion, and Suzuki.

MySQL specific shortcuts

July 24th, 2008

MySQL provides many extentions to SQL which help performance in many common use scenarios. Among these are INSERT … SELECT, INSERT … ON DUPLICATE KEY UPDATE, and REPLACE.

I rarely hesitate to use the above since they are so convenient and provide real performance benefits in many situations. MySQL has other keywords which are more dangerous, however, and should be used sparingly. These include INSERT DELAYED, which tells MySQL that it is not important to insert the data immediately (say, e.g., in a logging situation). The problem with this is that under high load situations the insert might be delayed indefinitely, causing the insert queue to baloon. You can also give MySQL index hints about which indices to use. MySQL gets it right most of the time and when it doesn’t it is usually because of a bad scheme or poorly written query.