create a cookie

Posted on 2004-10-22
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
    LVL 27

    Accepted 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";
    LVL 4

    Expert Comment

    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.
    LVL 6

    Expert Comment

    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

    Thank you guys
    LVL 27

    Expert Comment

    no problem :)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now