Solved

create a cookie

Posted on 2004-10-22
209 Views
Last Modified: 2008-03-06
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
0
Question by:submissiontechnology
    5 Comments
     
    LVL 27

    Accepted Solution

    by:
    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
     
    LVL 4

    Expert Comment

    by:KarlPurkhardt
    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
     
    LVL 6

    Expert Comment

    by:Eric
    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
     

    Author Comment

    by:submissiontechnology
    Thank you guys
    0
     
    LVL 27

    Expert Comment

    by:Diablo84
    no problem :)

    |)iablo
    0

    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