The following tutorial illustrates how to create a cookie with a sample code:
What is a Cookie?
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests for a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
--------------------------------------------------------------------------------
How to Create a Cookie
The setcookie() function is used to create cookies.
Note: The setcookie() function must appear BEFORE the <html> tag.
Syntax
setcookie(name, value, expire, path, domain);
Example
The following example sets a cookie named "uname" - that expires after ten hours.
<?php
setcookie("uname", $name, time()+36000);
?>
<html>
<body>
<p>
A cookie was set on this page! The cookie will be active when the client has sent the cookie back to the server.
</p>
</body>
</html>
---------------------------------------------------------------------
How to Retrieve a Cookie Value
When a cookie is set, PHP uses the cookie name as a variable.
To access a cookie you just refer to the cookie name as a variable.
Tip: Use the isset() function to find out if a cookie has been set.
Example
The following example tests if the uname cookie has been set, and prints an appropriate message.
<html>
<body>
<?php
if (isset($_COOKIE["uname"]))
echo "Welcome " . $_COOKIE["uname"] . "!<br />";
else
echo "You are not logged in!<br />";
?>
</body>
</html>