www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Trouble Setting Multiple Cookies in PHP 5

  1. #1
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357

    Question Trouble Setting Multiple Cookies in PHP 5

    I upgraded from 4 to 5. My login script, which worked absoutely fine on PHP 4, and which sets two cookies one right after the other, no longer works properly. The two cookies it sets are called username and password. It only sets whichever one is second in the list. For instance if I have it in this order:

    PHP Code:
    setcookie("username""$submitname"time()+60*60*24*30'/'); 
    setcookie("password""$submitpass"time()+60*60*24*30'/'); 
    Only the password cookie will be created.

    If I have it in this order:

    PHP Code:
    setcookie("password""$submitpass"time()+60*60*24*30'/');
    setcookie("username""$submitname"time()+60*60*24*30'/'); 
    Only the username cookie will be created.

    I have been very careful to make it least likely I was doing anything to cause this problem. To test the cookies all by themselves (and thus make sure something else in my script wasn't causing the problem) I made a PHP file with only this in it:

    PHP Code:
    <?PHP
    setcookie
    ("username""$submitname"time()+60*60*24*30'/'); 
    setcookie("password""$submitpass"time()+60*60*24*30'/');
    ?>
    My domain is dogrealm.net. I would first make sure I had deleted all cookies from dogrealm.net in my Firefox browser. Then I would visit that page. Then I would check my cookies in Firefox and only whichever cookie was set second would be there. To make sure I didn't just need to go to another page I would visit some other random site on the internet (e.g. google.com) and check my cookies again. Still the same thing.

    I was told to try setting my cookies as array values of one variable which looked like this:

    PHP Code:
    <?PHP
    setcookie
    ("userInfo[username]""$submitname"time()+60*60*24*30'/'); 
    setcookie("userInfo[password]""$submitpass"time()+60*60*24*30'/');
    ?>
    But it still did the exact same thing.

    Anybody know what's wrong?

  2. #2
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    Don't store passwords in a cookie, it is unsafe practice.

    try this and see what it outputs
    PHP Code:
    <?php
    setcookie
    ("password""$submitpass"time()+60*60*24*30'/') or die ("Can not set cookie");
    echo(
    "Cookie is set");
    die;
    ?>

  3. #3
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Don't worry, the password is encrypted. And here's the code I just ran:

    PHP Code:
    $submitname=evenstar7139;
    $submitpass=blahblah;
    setcookie("username""$submitname"time()+60*60*24*30'/')  or die ("Can not set cookie");
    echo(
    "Cookie is set");
    setcookie("password""$submitpass"time()+60*60*24*30'/')  or die ("Can not set cookie");
    echo(
    "Cookie is set");
    die; 
    This was the output:

    Cookie is setCookie is set

    But, alas, only password was set.

    I cleared my cookies, reversed the order of the setcookie commands, ran it and the same thing happened except it only set username. What the heck? I never had problems like this in PHP 4 0_0 Perhaps I have something set up wrong in my php.ini for 5? 5 was the first PHP version I ever set up myself.

    I just tried this in Internet Explorer and it did the same thing... So it's not just Firefox.
    Last edited by evenstar7139; 06-24-2007 at 05:42 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Wow no replies I wonder if this has stumped all the PHP gurus here as much as it stumped my PHP guru friend I am fairly competent at PHP and this problem makes absolutely NO SENSE to me.

  5. #5
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    I believe this has something to do with your headers.

    Examine your headers with a tool like this

  6. #6
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    This is what it outputted:

    HTTP Headers given when requesting your url

    HTTP/1.0 200 OK
    Connection: Keep-Alive
    Date: Mon, 25 Jun 2007 13:37:53 GMT
    Server: Quick 'n Easy Web Server
    Content-Length: 26
    Content-Type: text/html
    Client-Response-Num: 1
    Set-Cookie: username=ZXZlbnN0YXI3MTM5; expires=Wed, 25-Jul-2007 13:37:53 GMT; path=/
    X-Powered-By:

    I notice the password cookie isn't in there.

  7. #7
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    After looking at the set_cookie function on PHP.net I saw a comment with this in it..

    PHP Code:
    function set_cookie_new($Name$Value ''$MaxAge 0$Path ''$Domain ''$Secure false$HTTPOnly false) {
      
    header('Set-Cookie: ' rawurlencode($Name) . '=' rawurlencode($Value)
                            . (empty(
    $MaxAge) ? '' '; Max-Age=' $MaxAge)
                            . (empty(
    $Path)   ? '' '; path=' $Path)
                            . (empty(
    $Domain) ? '' '; domain=' $Domain)
                            . (!
    $Secure       '' '; secure')
                            . (!
    $HTTPOnly     '' '; HttpOnly'), false);

    Try using that set_cookie_new() function to see if that works and then try to figure out why set_cookie() is not working correctly?

    Maybe there is a misconfiguration somewhere?

    Is there anything in the PHP.ini to do with headers / cookies that looks fishy?

    I will check later when I get home if I have time

    Good luck m8.

  8. #8
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Just tested your code on our webserver..

    Code:
    HTTP/1.1 200 OK
    Connection: close
    Date: Mon, 25 Jun 2007 15:07:32 GMT
    Server: Apache/2.2.0 (Fedora)
    Content-Length: 0
    Content-Type: text/html; charset=ISO-8859-1
    Client-Response-Num: 1
    Set-Cookie: username=deleted; expires=Sun, 25-Jun-2006 15:07:31 GMT; path=/
    Set-Cookie: password=deleted; expires=Sun, 25-Jun-2006 15:07:31 GMT; path=/
    X-Powered-By:
    Working fine here

  9. #9
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    It worked fine on my old server too. I wonder if something is wrong in the PHP.ini file...can you take a look at it? I'll take out anything that would compromise my security to show a stranger (no offense!) and then send you the whole rest of it.

  10. #10
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Just got home, did you have any luck with set_cookie_new()?

    Post your PHP.ini here or PM me it you like.

  11. #11
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    MrCoder, did you get my e-mail? I sent my PHP.ini file to you.

  12. #12
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Yes sorry mate, didnt see anything wrong with it.

    I guess you havent made any more progress?

    Did you try that code I posted above, the replacement function?
    Did that function produce the same effect?

  13. #13
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    That code had the same effect, yes. Do you think maybe my server software could be causing this problem? I have the Quick 'n Easy Web Server and it's running on Windows XP 32 bit. Is it possible I have something set wrong in it?

  14. #14
    Join Date
    Jun 2007
    Posts
    112
    It could be that. Try apache. Apache2 is really easy to install and configure under windows.

  15. #15
    Join Date
    Jul 2012
    Posts
    2
    I got the same problem using the internal server of WaterProof PHPEdit v5.0.
    I than moved the site to Apache and got rid of the problem!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles