How to Make an HTTP Cookie

Level of difficulty: Difficult

An HTTP cookie is a way to store and retrieve information on a client system from a server. It is a file set on a system where data is saved so that when navigating anywhere across a website, information would not be lost. Usually, cookies are removed after closing the browser, but there exist cookies that remain even after exiting the browser. There are three basic steps in creating cookies: Setting, Reading, and Calling.

Materials Needed:
- Knowledge in HTML and CGI or JavaScript
- existing HTML website source codes
- HMTL editor or any text editor
Step 1
Edit an existing HTML source code with any editing program. This can be done over an Integrated Development Environment (IDE) when working with a big website composed of many objects. Small websites can be edited using Microsoft Front Page or any text editor such as Notepad, WordPad, or Microsoft Word.
Step 2
Open the default homepage or any page that is wished to be the one setting a cookie on a client machine when loaded on a browser. Usually, this page has the file name 'index.html' or 'index.htm'.
Step 3
Set the cookie at any part on the Web page. Cookies are set by the browser when 'Set-Cookie' is encountered. In this step, the variables for the cookie count, expiration, path, and domain are set. The domain is site that would set the cookie and is the only site to be able to read the cookie.
Step 4
Using a script, make the code that will write the cookie. Normally, JavaScript is used in doing this. Insert the code into the page by beginning with <script language='JavaScript'> and ending it with </script>.
Step 5
Define the cookie by creating a variable and inside the script. An example of a cookie name would be 'my_cookie'. Set this to the name of the cookie using my_cookie = 'My_Cookie' or any other name.
Step 6
Write the function afterwards. Do this by using the 'function' keyword. As an example, 'function write_my_cookie() { }'. Inside the function would be the codes to check for existing cookies, create a new cookie, and update existing cookies.
Step 7
If there are no cookies, a code to create a cookie should be present. If an existing cookie is encountered, the code should update it.
Step 8
When the script to set the cookie is done, the cookie needs to be read for it to be used. Using another script like the one in Steps 4 and 6 creates the function to read the script. An example would be 'function update_cookie() { }'. Inside this function are the codes to check if a cookie exists, read a specific part of the cookie, and return the information on the cookie to the server.
Step 9
Now, a call to the cookie may be done at any page on the website. To do this, choose any page so that when a link to that page is clicked, the function to read and update the cookie will be called. This function is the same with the function in Step 8. In any page, create a script with the function 'document.write(update_cookie());' inside it. When that page loads, the function is executed and the cookie will be read and updated.

You are currently viewing How to Make an HTTP Cookie on Uniblue's how to archive.