create a cookie

Hello

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
submissiontechnologyAsked:
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.

Diablo84Commented:
To set the cookie (if your username was assigned to $username and your passwards assigned to $password):

setcookie("cookiename","$username|$password",time()+31556926);

or if your password needs to be MD5 hashed:

setcookie("cookiename",$username."|".md5($password),time()+31556926);

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

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
KarlPurkhardtCommented:
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.
0
EricCommented:
take a look at your previous question in the Web Development Forum as well..

PHP:

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).
0
submissiontechnologyAuthor Commented:
Thank you guys
0
Diablo84Commented:
no problem :)

|)iablo
0
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
PHP

From novice to tech pro — start learning today.