Results 1 to 4 of 4

Thread: [PHP] Help with cookie

  1. #1
    Join Date
    May 2012
    Posts
    29
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default [PHP] Help with cookie

    I have a "visitor counter" code:
    PHP Code:
         <?php
         $fn 
    'index_files/licznik.txt';
         
    $counter = (file_exists($fn) ? (int)file_get_contents($fn) : 0) + 1;
         
    $cookiename 'licznik';
         if ( ! isset(
    $_COOKIE[$cookiename])){
             
    file_put_contents($fn$counter);
             
    setcookie($cookiename'1'time()+60*60*24);
         }
         
    ?>
    It was okay on WAMP, but online I'm getting "headers already sent" warning, so I must modify that cookie so it works. Warning says about line 7.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Headers of any kind including cookies are sent before HTML output. It doesn't technically hurt anything to send extra headers, but they won't work at all (and in fact PHP doesn't even bother sending them if the headers have already been sent).

    How to fix it: you cannot have any output whatsoever (even a single space or line break) before the cookie (or other header) is sent. That's all. So place the setcookie() command before any HTML output, and your script will work fine.

    In the event that you must have the setcookie() command after your HTML begins (for organizational reasons) you'll need to significantly reorganize your code so that this isn't the case. It might involve an output buffer, using templating, or some other method to avoid the problem.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    Lestatt (06-01-2012)

  4. #3
    Join Date
    May 2012
    Posts
    29
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    It seems I can't do as you say, because I'm including counter.php file in the footer of html file.
    Meanwhile i managed to set this up and working by using buffer. It works so far, if there's any problems I'll be back ;]

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    As a general note, output buffers are generally considered bad to use because of memory issues and because, again generally, they imply that you don't know the better way to code them. On the other hand (especially for something like this) they can be very useful and help you avoid rewriting everything. This is just something to keep in mind. If your site is working, doesn't seem to be taxing your server too much, and you're happy with it, don't worry about it too much. (I'm just cautioning you against using output buffers too often, as is generally seen as bad practice.)

    I'd need to look at a your entire setup to figure out exactly what to do. But using include() is equivalent to putting this code at the bottom of another file. That detail is irrelevant.
    Why exactly can't you put this code at the top of your page?
    Usually the method used in a situation like this is to run the computations (decisions) first, then present the output later. So whatever information determines, for example, whether that file exists (I'm assuming the script above this might create that file...?), then you can run that first, but not the output from the earlier part.
    Then after you've done all of the decisions and setup, you can go through and actually present the output to the browser.
    This is basically a kind of templating, and as your website increases in complexity, it's worth thinking about it that way.

    Think of the information structure of your page. Determine the decisions at the top, such as whether the counter will increase by one, or whether you will show an ad, or which page will be displayed. Then after all of your information is ready, go through a standard output stage using a template.
    Often this is integrated into a more complex set of layers, such as using a single template for every page on your site (via includes).

    If you want to take a look at a (complicated) setup like this, then downloading forum software (for example, SMF or PhpBB, both free), would be an example to look at. There are a lot of decisions made at the beginning, then the processing of submitted data and retrieving the information (like posts) to display, then finally putting all of that into the template.



    One completely different approach would be to use OOP (object oriented programming). There's a nice little trick with the destruct() method for a class, so that you can create an object (here it could be a counter) that has properties (variables) inside it and you can do what you need at the beginning. Then automatically when it is destroyed (eg, when the page has finished and the script is stopping) it could output the remaining stuff (as defined in your destruct() method/function) automatically. I use this on one of my website to create the HTML frame. I output <html>....<body> at the top of the page and at the same time add the </body></html> lines for the end-- in a destruct() method for an object. This means that automatically when my script terminates, the HTML is also terminated automatically by the script. There is no code at all in my pages to say "now add the HTML close tag", but it always happens automatically.


    Most of what I just said is beyond what is relevant for your current project. But it's something to consider if you want to fully understand what to do in situations like this in the future and some alternatives.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •