FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Join us as Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
 

PHP Tutorial: Creating Cookies using PHP

jawahar
5/20/2005 5:00:22 PM, Views: 2689
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>
Next Steps:
Add this Tutorial to:
Blink Blink del.icio.ous Del.icio.us Digg Digg
Fark Fark Furl Furl Google Google
Reddit Reddit Simpy Simpy Spurl Spurl
Technorati Technorati Windows Live Win Live Yahoo Yahoo
Rate Me!
Avg Visitor Rating: Average Visitor Rating is 5 out of 5
Number of Ratings : 2 Votes
Rate:
Send Private MessageSend Message
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
        (Link Rules) 
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
Options
PHP RSS Feed
Most Popular Tutorial
Most Popular Solution
No Records!
Top Rated
Top Rankers
Overall
1. jawahar (500)
Yearly -2008
No Rankings!
Expertsforge Sponsors
bnrtop