Introduction to cookie creation and use.

What is a cookie?

A cookie is a file that is stored on your computer that keeps track of nearly anything. It can be used to store usernames, passwords, location, and more.

What is a cookie used for?

A cookie is used to hold a common variable across every page that the cookie is checked on. This is useful for creating a login system and things of that nature. How do I create a cookie? To create a cookie, all you need is one line of code:

setcookie("ymatBBu", $username, time()+3600);

The first part, "ymatBBu" is the name of the cookie, the second part is the value of the cookie, and the third is the expiration date in seconds from the creation time. To delete a cookie, simply set the time to a negative number rather than a positive number.

I have a cookie...what now?

Once the cookie is created, you will need to put this at the top of all the pages you wish to use the cookie on:

ob_start();

It must go at the very top, before all other code. To grab the cookie value that you set for the cookie, you will need to do this:

$uza = ($_COOKIE['ymatBBu']);

The previous line of code set the variable $uza to the value of the cookie that we set earlier (in this case, the value was the value of $username) For a more secure login system, you will need to make 2 cookies, 1 to store the username and the other to store the password. Check both to see if they match with those of the database, and if they do, then you may set the variable to the cookie value.