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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.