• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

create a cookie


I need to create a cookie that will save a username and password that will never expire and then I will need to read and disply that cookie at a later dtae.

Can anyone show me how
1 Solution
To set the cookie (if your username was assigned to $username and your passwards assigned to $password):


or if your password needs to be MD5 hashed:


note: AFAIK cookies must have an expiration date so i have set it to a year

To read the cookie:

if (isset($_COOKIE['cookiename'])) {

 $data = explode("|",$_COOKIE['cookiename']);
 $username = $data[0];
 $password = $data[1];

 echo "Username: $username<br>\nPassword: $password<br>\n";
to set a cookie use

setcookie("username", "Karl", time() + $expire);  

$expire would be the time in seconds for when this cookie will expire, so for example if u set $expire to 10 the cookie will expire 10 seconds after being created.

make sure this is set at the beginning of the page before any headers are sent

then to retrive the cookie

// make sure the cookie exists first
if (isset($_COOKIE['username'])) {
  echo "You username is ".$_COOKIE['username'];

thats the basics of it, if you want to learn more about cookies you should check out the php manual.
take a look at your previous question in the Web Development Forum as well..


time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes).
submissiontechnologyAuthor Commented:
Thank you guys
no problem :)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now