SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Thread: PHP cookie help

  1. #1
    SitePoint Member
    Join Date
    Mar 2004
    Location
    MN
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry PHP cookie help

    Hello,

    I am trying to get this PHP script to work

    <?
    $visitcount = $HTTP_COOKIE_VARS["visits"];
    if( $visitcount == "") $visitcount = 0;
    else $visitcount++;
    setcookie("visits",$visitcount);
    print "This is visit number " . $visitcount;
    ?>



    But I am having problems.

    Notice: Undefined index: visits in /net/home-ANet/c/pc60/Web/PersonalPages/php/cookie.php on line 2

    Warning: Cannot modify header information - headers already sent by (output started at /net/home-ANet/c/pc60/Web/PersonalPages/php/cookie.php:2) in /net/home-ANet/c/pc60/Web/PersonalPages/php/cookie.php on line 5
    This is visit number 0


    Thank You,

    Paul

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2004
    Location
    Sofia, Bulgaria
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for the notice:
    PHP Code:
    // you can use $_COOKIE array, because it's automatically global in any scope.
    // $HTTP_COOKIE_VARS contains the same initial information, but is not an autoglobal.
    if (!empty($_COOKIE['visits'])) {
      
    $visitcount $_COOKIE['visits'];
      
    $visitcount++;
    } else {
      
    $visitcount 0;

    for the warning:
    if you are outputing anything to the client (i.e. print, echo, etc.) then you cannot print a header because they have already been sent to the client (this happens right before you echo your content). this error is also shown where there are extra spaces outside the php tags (<? ?>). check your code if something is sent to the browser before you use setcookie() function.

  3. #3
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this line

    Notice: Undefined index: visits in /net/home-ANet/c/pc60/Web/PersonalPages/php/cookie.php on line 2

    caused the header error.

    you can turn notices off in the ini file or in the script.

    error_reporting = E_ALL & ~E_NOTICE

    an uncomment it. Comment all the others.

    From within the code:

    PHP Code:
    error_reporting(E_ALL & ~E_NOTICE); 
    success is not by chance, it is by choice.

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a fun one-liner for ya :
    PHP Code:
    // Cookie will be set for a month
    setcookie('visits', ($visit_num = (empty($_COOKIE['visits'])) ? : ++$_COOKIE['visits']), time()+18144000);

    echo 
    'This is visit number '.$visit_num

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sahajin
    you can turn notices off in the ini file or in the script.
    Turning off error reporting is not exactly a great substitute for fixing errors.

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2004
    Location
    Sofia, Bulgaria
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattjacob
    Here's a fun one-liner for ya :
    PHP Code:
    // Cookie will be set for a month
    setcookie('visits', ($visit_num = (empty($_COOKIE['visits'])) ? : ++$_COOKIE['visits']), time()+18144000);

    echo 
    'This is visit number '.$visit_num
    that's really nice

  7. #7
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattjacob
    Turning off error reporting is not exactly a great substitute for fixing errors.
    I didn't say turn it off, notices are not errors. and look at teh code it doesn't turn anything off but notices.
    success is not by chance, it is by choice.

  8. #8
    SitePoint Evangelist
    Join Date
    Feb 2004
    Location
    Sofia, Bulgaria
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sahajin
    I didn't say turn it off, notices are not errors. and look at teh code it doesn't turn anything off but notices.
    on servers in many cases errors are not displayed to the screen, but logged into log files and in many cases all sort of errors are logged, this include the notices.. log files sometimes became really huge of that notices..
    i think mattjacob means that it's better to code the script more carefully so it doesn't produce such notices.. to use more frequently empty() and isset() for example

  9. #9
    SitePoint Member
    Join Date
    Mar 2004
    Location
    MN
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have revised the code, so now I have this
    <?php
    if (!empty($_COOKIE['visits'])) {
    $visitcount = $_COOKIE['visits'];
    $visitcount++;
    setcookie("visits", $visitcount);
    } else {
    $visitcount = 0;
    }
    print "This is visit " . $visitcount;
    ?>

    I get no errors but when I reload the page the counter stays at zero.

  10. #10
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then try this
    PHP Code:
    <?php
    if (!empty($_COOKIE['visits'])) {
    $settime time() + (60 60 24 365); // 1 year form now
    $visitcount $_COOKIE['visits'];
    $visitcount++;
    setcookie("visits"$visitcount$timeset"/");
    } else {
    $visitcount 0;
    }
    print 
    "This is visit " $visitcount;
    ?>
    success is not by chance, it is by choice.

  11. #11
    SitePoint Member
    Join Date
    Mar 2004
    Location
    South Australia
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sahajin
    this line

    Notice: Undefined index: visits in /net/home-ANet/c/pc60/Web/PersonalPages/php/cookie.php on line 2

    caused the header error.

    you can turn notices off in the ini file or in the script.

    error_reporting = E_ALL & ~E_NOTICE

    an uncomment it. Comment all the others.

    From within the code:

    PHP Code:
    error_reporting(E_ALL & ~E_NOTICE); 
    Don't tell people how to ignore notices, tell them how to stop them. Ignoring some notices can prove to be a security risk.

    if (!empty()) works fine mind

  12. #12
    SitePoint Member
    Join Date
    Mar 2004
    Location
    MN
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have now tried this code
    <?php
    if (!empty($_COOKIE['visits'])) {
    $settime = time() + (60 * 60 * 24 * 365); // 1 year form now
    $visitcount = $_COOKIE['visits'];
    $visitcount++;
    setcookie("visits", $visitcount, $timeset, "/");
    } else {
    $visitcount = 0;
    }
    print "This is visit " . $visitcount;
    ?>
    What I get now is the display is 0 but the counter never goes up from there. Any advise.
    Paul

  13. #13
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you make sure you have a cookie set? you should see it in your browser if you have firefox/mozilla or in your cookies folder fro IE.

    if so try this
    PHP Code:
    <?php
    if (!empty($_COOKIE['visits'])) {
    $settime time() + (60 60 24 365); // 1 year form now
    $visitcount $_COOKIE['visits'];
    $visitcount++;
    setcookie("visits"$visitcount$timeset"/");
    } else {
    $visitcount 0;
    }
    print 
    "This is visit " $_COOKIE['visits'];
    ?>
    also do you know if register_globals is OFF or ON?
    success is not by chance, it is by choice.

  14. #14
    SitePoint Member
    Join Date
    Mar 2004
    Location
    MN
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    register_globals is off.
    I do not see a cookie in the folder C:\Documents and Settings\Paul\Cookies. Is this the correct area to be looking?

  15. #15
    SitePoint Member
    Join Date
    Mar 2004
    Location
    MN
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried this same code on a server with register_globals = on and I get the same results.
    0 is displayed but I get nothing after that.

  16. #16
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there or temporary internet folder.

    god, how stupid am I
    PHP Code:
    <?php
    $settime 
    time() + (60 60 24 365); // 1 year form now
    if (!empty($_COOKIE['visits'])) {
        
    $visitcount $_COOKIE['visits'];
        
    $visitcount++;
        
    setcookie("visits"$visitcount$timeset"/");
    } else {
        
    $visitcount 0;
        
    setcookie("visits"$visitcount$timeset"/");
    }
    print 
    "This is visit " $_COOKIE['visits'];
    ?>
    of course it was empty, we never set in the first place.
    success is not by chance, it is by choice.

  17. #17
    SitePoint Member
    Join Date
    Mar 2004
    Location
    MN
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the same thing with that code too. Does register_globals need to be on???

  18. #18
    SitePoint Member
    Join Date
    Mar 2004
    Location
    MN
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second time I visit the site there is a cookie set.
    The first time I get the error saying visits is undefined.

  19. #19
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blaze82
    The second time I visit the site there is a cookie set.
    The first time I get the error saying visits is undefined.
    And the reason for that is because cookies don't become visible until the page is reloaded. Edit: what was wrong with the code I posted earlier?

  20. #20
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want more code, you could write it this way:
    PHP Code:
    if (empty($_COOKIE['visits']))
    {
        
    $count 0;
    }
    else
    {
        
    $count = ++$_COOKIE['visits'];
    }

    setcookie('visits'$counttime()+18144000);

    echo 
    'This is visit #'.$count
    And that could also be rewritten like so:
    PHP Code:
    $count = (empty($_COOKIE['visits'])) ? : ++$_COOKIE['visits'];
    setcookie('visits'$counttime()+18144000);

    echo 
    'This is visit #'.$count


Bookmarks

Posting Permissions

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