Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


create a cookie

Posted on 2004-10-22
Medium Priority
Last Modified: 2008-03-06

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
Question by:submissiontechnology
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 27

Accepted Solution

Diablo84 earned 2000 total points
ID: 12381191
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";

Expert Comment

ID: 12381208
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.

Expert Comment

ID: 12381274
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).

Author Comment

ID: 12381328
Thank you guys
LVL 27

Expert Comment

ID: 12381352
no problem :)


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question