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 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Having Cookie Problems...

    I had this working before... now it won't work! Anyways, basically what my code does is set a cookie with a persons name inside to be retreived later... anyways, here's a snippet of my code: (If you want the whole sha-bang, just say so)

    Code:
    $cookie_name = "name_cookie";
    $value = "".$name."";
    $inTwoMonths = 60 * 60 * 24 * 60 + time(); 
    setcookie($cookie_name, $value, $inTwoMonths);
    And to retreive said cookie:
    Code:
    if(isset($_COOKIE[name_cookie'])) {
    $name_chat = $_COOKIE['name_cookie'];
    echo "Name: <b>".$name_chat."</b><br>";
    } else {
    echo "You need to register before you can chat!</div>";
    }
    It worked before, but after I've reordered my directories around... it will not work. I snooped around and found this, but I'm not sure if its relevant. My pages are located in different directories, specifically these:

    http://myip/nc_fc_db/nch_users/nch_process_addfc.php -- Cookie is SET here
    http://myip/nc_fc_db/nch_chat/nch_chat.php -- Cookie is READ here

    Any help would be greatly appreciated. Thanks!
    Attached Thumbnails Attached Thumbnails Having Cookie Problems...-pic.jpg  

  • #2
    New Coder
    Join Date
    Jun 2006
    Location
    USA
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use print_r on $_COOKIE in the if statement where the cookie name is set to find out what's in the hash.
    "Some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems."
    --Jamie Zawinski

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I used the following code:

    Code:
    $name_chat = print_r ($_COOKIE['name_cookie']);
    echo $name_chat;
    and I got:
    Notice: Undefined index: name_cookie in C:\pathtomyserverstuff\nc_fc_db\nch_chat\nch_chat.php on line 44
    1

    Did I do that correctly?
    Last edited by Drevin; 06-17-2006 at 06:02 AM.

  • #4
    New Coder
    Join Date
    Jun 2006
    Location
    USA
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can't store print_r w/o output buffering, in this case, it just echo'd it to the browser, thus giving the same effect if you had not tried to store it.

    In any case, I just noticed this code:
    PHP Code:
    $value "".$name.""
    This assignment doesn't make sense. First of all, you concatenate two empty string values on either side of the variable, which does nothing. Second, more important, you use $name, but you hadn't declared it above. I assume that means you're using register globals, which is a risk.
    Quote Originally Posted by php.net
    When on, register_globals will inject your scripts with all sorts of variables, like request variables from HTML forms. This coupled with the fact that PHP doesn't require variable initialization means writing insecure code is that much easier. It was a difficult decision, but the PHP community decided to disable this directive by default. When on, people use variables yet really don't know for sure where they come from and can only assume. Internal variables that are defined in the script itself get mixed up with request data sent by users and disabling register_globals changes this.
    Your error is almost 100% likely because that $name variable has nothing in it when using setcookie().

    Change your code to the following
    PHP Code:
    <?php
    if ( isset($_POST['name']) )
       
    setcookie('cookie_name'strip_tags($_POST['name']), ((60^3)*24+time());

    // This is a short hand if() statement, doing what yours did above
    // it's called the ternary operator
    echo isset($_COOKIE['cookie_name']) ? 'Welcome, '.htmlentities($_COOKIE['cookie_name']) : 'You need to register';
    ?>
    Note that your HTML register form must have method="post" in the form tags, as well as making sure the name input element matches $_POST['whatever']. If using my code, you'd need <input type="text" name="name" ... />
    Last edited by Curtis D; 06-17-2006 at 09:33 AM.
    "Some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems."
    --Jamie Zawinski

  • #5
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You forgot to add a singlequote ' in your if-statement:
    PHP Code:
    if(isset($_COOKIE[name_cookie])) 
    change it to,
    PHP Code:
    if(isset($_COOKIE['name_cookie'])) 

  • #6
    New Coder
    Join Date
    Jun 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, I fixed it already, and it was a path error. I didn't know you had to assign paths to cookies, and that was my problem. The cookie was created in a different path than where it is used, thus it was restricted to where it was created. When I assigned it a path of the entire domain, it worked perfectly.

    Thanks for the help however, and I'll use that if statement to shorten my code.

    EDIT: Oh, and if you'll recall, that was a SNIPPET of my code. I had already declared $name elsewhere, all I did was put in the relevant code that had the cookie in it.
    Last edited by Drevin; 06-18-2006 at 02:06 AM.

  • #7
    New Coder
    Join Date
    Jun 2006
    Location
    USA
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good job on getting it resolved, Drevin
    "Some people, when confronted with a problem, think, 'I know, I'll use regular expressions.' Now they have two problems."
    --Jamie Zawinski


  •  

    Posting Permissions

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