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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy don't get cookies

    i've searched around as well as checked on php.net... i still don't think i've got it with cookies.
    i've got a page '1.php', that has a form with a userId textbox. Users insert their id in 1.php, and submit the form. then 2.php comes up and they can see their id. when they go back to 1.php and refresh it, their id's still there. that's fine. it means the cookie did kind of work.
    but if they go around somewhere else or close the window or something, and then come back, the cookied id's gone again.


    here's my code for 1.php:
    Code:
    <?php
    if($_COOKIE['id']){
    $cookieId=$_COOKIE['id'];
    }
    ?>
    .
    .//do other stuff...
    .
    <?php
    echo("<input type='text' class='txt' name='userid' value='{$cookieId}' onfocus=\"this.value=='ID' ? this.value=='' : null;\" />");
    ?>
    and on 2.php is where the cookie is actually set:
    Code:
    <?php
    
    if($_GET){
    setcookie("id",$_GET['userid'],time()+60*60*24*30);
    }
    echo("Welcome, {$_COOKIE['id']}!");
    
    ?>
    'If you don't stand for something, you'll fall for anything.'

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know much about cookies myself but it sounds like the cookie is valid only on the current visit which would indicate that the time varriable for the cookie isn't working right. Might want to read up about how that part works.


    Jason

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    setcookie("id",$_GET['userid'],time()+60*60*24*30, "/" , "./yourdomainname", 0);

    Try that instead. Don't forget that you'll need the period (.) before the slash in front of your domainname.

    The attribute after the time basically said the cookie is valid all over the site, not just a particular folder.

    Duniyadnd

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    actually i just tested my old code again. what happens with that is that on one refresh, the cookie works, but on the second refresh it's gone. and on moving around, it's definitely gone.

    now with adding ,"/","./http://127.0.0.1/",0 in the setcookie function, it doesn't remember the id at all, after any refreshes...

    am I calling the cookie wrongly on 1.php or something?
    'If you don't stand for something, you'll fall for anything.'

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no, the desitnation is wrong...that cookie you put on someones computer is using the "whole site" set at "127.0.0.1" which is also localhost so the cookie would apply to the users computer instead of your server. Try and change the IP to match the IP or URL of your site...


    Jason

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oops sorry, maybe i should have clarified...this is not on any 'real' site, i've got apache on my machine and i'm testing cookies on my own localhost
    'If you don't stand for something, you'll fall for anything.'

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    okay it's like this.
    shoutbox.php is the file where the cookied id should appear.
    shouts.php is a file in an iframe within shoutbox.php

    shoutbox.php:
    Code:
    <?php
    if($_COOKIE['id']){
    $cookieId=$_COOKIE['id'];
    echo($cookieId."<BR>");
    var_dump($_COOKIE);
    }else{$cookieId="ID";}
    ?>
    .
    .
    .
    <?php
    echo("<input type='text' class='txt' name='shouter' value='{$cookieId}' onfocus=\"this.value=='ID' ? this.value=='' : null;\" />");
    ?>
    .
    .
    .
    and shouts.php:
    Code:
    ...
    
    if($_GET){
    $newshout=$_GET['shouttext'];
    $newname=$_GET['shouter'];
    setcookie("id",$_GET['shouter'],time()+60*60*24*30, "/" , "./asaaki.ithium.net", 0);
    
    ...
    i kinda had the idea cookies were really easy...
    Last edited by ASAAKI; 05-25-2003 at 06:17 AM.
    'If you don't stand for something, you'll fall for anything.'

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I have no idea what's wrong, I've had cookies working on my localhost with the very first snippet you showed. Maybe it's something to do with your cookie settings on your browser? and by the looks of the slowness of replies, noone else can see what's wrong


  •  

    Posting Permissions

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