SETCOOKIE Problem

Discussion in 'PHP' started by Pudge1, Aug 4, 2010.

  1. #1
    This is on login.php

    
        setcookie('LOGGED_IN','True_',time()+3600*24*3);
        setcookie('USERNAME',$_POST['username'],time()+3600*24*3);
    
    Code (markup):
    This is on index.php

    
    echo '<!--' . $_COOKIE['LOGGED_IN'] . '-->';
    echo '<!--' . $_COOKIE['USERNAME'] . '-->';
    
    Code (markup):
    Along with another script to test the cookies, however it doesn't show anything almost like the cookie isn't being set. I have looked over the script many times but I don't see what is wrong. print_r($_COOKIE) also comes up blank.

    I don't see anything wrong with the syntax of either thing.
     
    Pudge1, Aug 4, 2010 IP
  2. siothach

    siothach Active Member

    Messages:
    656
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    your browser enabled cookie?
     
    siothach, Aug 4, 2010 IP
  3. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    Yes it does.
     
    Pudge1, Aug 4, 2010 IP
  4. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    lol maybe because of these?
    
    '<!--' 
    
    Code (markup):
    And
    
     '-->'
    
    Code (markup):
    Those are syntax for comments on HTML.
     
    Rainulf, Aug 4, 2010 IP
  5. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    Yes and when I look at the source code it is blank comments. Also the problem is that the cookie is being set.
     
    Pudge1, Aug 4, 2010 IP
  6. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #6
    Try to put it in double quotes like this:
    
    setcookie("LOGGED_IN","True_",time()+3600*24*3);
    setcookie("USERNAME",$_POST['username'],time()+3600*24*3);
    
    PHP:
    EDIT: I've tested it, I don't think there's any difference in being single or double quotes but it should work no problem either way. =/ The first time, there were no variables present, it was blank as you said. Then I refreshed it and it just magically appeared. I'm not exactly sure why though, gotta research that.

    You can alternately use PHP sessions if you continue to have issues with it. :)
     
    Last edited: Aug 4, 2010
    Rainulf, Aug 4, 2010 IP
  7. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #7
    That was a good idea but sadly it didn't work. For some reason it isn't working for me. I even stuck it on the same page right before where it is supposed to echo the cookie contents and it doesn't work.
     
    Last edited: Aug 4, 2010
    Pudge1, Aug 4, 2010 IP
  8. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #8
    i also have some problem when i try to make a cookie like:
    setcookie("USERNAME",$_POST['username'],time()+3600*24*3);

    well i tried this:

    
    $name="$_POST[username]";
    setcookie("USERNAME",$name,time()+3600*24*3);'
    
    PHP:
    and also i don;t think two cookies can be set at once.
     
    arpit13, Aug 5, 2010 IP
  9. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #9
    If you read this, under Common Pitfalls, it says:
    So I guess that's why. =/

    I believe it can be set twice. But there's a limit per domain.
     
    Rainulf, Aug 5, 2010 IP
  10. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #10
    Ugh, I don't like sessions but I guess I will have to use them anyway because these cookies just absolutely won't set. This is really odd... now the sessions aren't setting.
     
    Last edited: Aug 5, 2010
    Pudge1, Aug 5, 2010 IP
  11. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #11
    
           $o_username = $_POST['username'];
    
           session_start();
    
           $_SESSION["LOGGED_IN"] = "True_";
           $_SESSION["USERNAME"] = $o_username;
    
           session_destroy();
    
    Code (markup):
    For some reason this isn't setting.
     
    Pudge1, Aug 5, 2010 IP
  12. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #12
    PHP sessions are way more secure than cookies because it stores cookies on the server, not the user's computer where they full access - they can modify/delete/add cookies.

    Try this:
    
    <?php
           session_start();
    
           $_SESSION['LOGGED_IN'] = "True_";
           $_SESSION['USERNAME'] = "lol";
           
           echo $_SESSION['LOGGED_IN'];
           echo $_SESSION['USERNAME'];
           
           unset($_SESSION['USERNAME']);
           unset($_SESSION['LOGGED_IN']);
    
    ?>
    
    PHP:
     
    Rainulf, Aug 5, 2010 IP