Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: cookie help

  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts

    cookie help

    I have the following

    $username="test";
    $username="pass";
    setcookie("username", $username, time()+3600);
    setcookie("password", $password, time()+3600);



    echo "<br>username:".$_COOKIE["username"];
    echo "<br>password:".$_COOKIE["password"];



    echo "Thank You. Your username is:".$username." <br>Your Password is:".$password;


    for some reason the cookies are not saving -- what am i doing wrong?

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Are both not showing?

    You've named the password variable wrong on line 2
    $password="pass";

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts
    no that was just a copy and paste error

    it is not showing any of the set cookies

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Are you setting them before anything is outputted to the browser? Any errors or notices showing?

    What does

    print_r($_COOKIE);

    return?

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    Are the following two echo statements on the same page as the setcookie() statements?
    PHP Code:
    echo "<br>username:".$_COOKIE["username"];
    echo 
    "<br>password:".$_COOKIE["password"]; 
    If so, $_COOKIE[...] values are not set until the browser sends the cookies back to the web server. This won't happen until the page is refreshed or you browse to a different page.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    you can simply refresh the page as soon as the cookies have been set by adding
    PHP Code:
     header("Location:http://www.yoursite.com/yourpage.php");
    //this will reload the page and echo the cookies that were send :) 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •