Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Creating a log

  1. #1
    Join Date
    Dec 2006
    Posts
    74
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating a log

    How would you create a .log file (that includes time + date + IP) for every time someone visit a page?

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    $handle = fopen("includes/myfile.log","w+");
    $time = date("d-m-Y");
    $ip = $_SERVER["REMOTE_ADDR"];
    $data = $time." -- IP: ".$ip;
    fwrite($handle,$data);
    fclose($handle);
    - Mike

  3. #3
    Join Date
    Dec 2006
    Posts
    74
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    thanks :d

  4. #4
    Join Date
    Dec 2006
    Posts
    74
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    how would i make it say what page they where looking at?

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Add this to the query:
    Code:
    $_SERVER["PHP_SELF"]
    - Mike

  6. #6
    Join Date
    Dec 2006
    Posts
    74
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    thanks

  7. #7
    Join Date
    Dec 2006
    Posts
    74
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    so would it be:
    Code:
    <?
    $handle = fopen("zineviews.html","w+");
    $time = date("d-m-Y");
    $ip = $_SERVER["REMOTE_ADDR"];
    $data = "DATE: ".$time."<br>IP: ".$ip. .$_SERVER["PHP_SELF"]"<br>-------------------</br><br>";
    fwrite($handle,$data);
    fclose($handle);
    ?>

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    <?
    $handle = fopen("zineviews.html","w+");
    $time = date("d-m-Y");
    $ip = $_SERVER["REMOTE_ADDR"];
    $data = "DATE: ".$time."<br>IP: ".$ip .$_SERVER["PHP_SELF"]."<br>-------------------</br><br>";
    fwrite($handle,$data);
    fclose($handle);
    ?>
    You had some of your dots in the wrong place, everything else is fine.

    Note that this script will erase the log everytime, so use this:
    Code:
    <?
    $handle = fopen("zineviews.html","w+");
    $time = date("d-m-Y");
    $ip = $_SERVER["REMOTE_ADDR"];
    $data = file_get_contents("zineviews.html") . "DATE: ".$time."<br>IP: ".$ip .$_SERVER["PHP_SELF"]."<br>-------------------</br><br>";
    fwrite($handle,$data);
    fclose($handle);
    ?>
    - Mike

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Don't read the whole thing into memory and then write it back... the underlying OS supplies an append ability for files, which would be much more efficient (especially if you end up with gigabyte log files... bye-bye, server's memory!). Also, HTML's kind of tricky here, because you have to insert the data before the ending </body> and </html> tags, rather than simply appending. It would be easier to use a plain text file. Also, if you use PHP_SELF, it will break if used as an include (which is the best way to use it). Try REQUEST_URI instead -- that will give a better overview of what was requested, too, and will include GET variables.
    Code:
    <?php
      $f = fopen('zineviews.txt', 'a');
      $entry = <<< END
    Log entry, &#37;s:
      A view of %s occurred from IP %s.
    
    END;
      $entry = sprintf($entry, date('r'), $_SERVER['HTTP_REQUEST_URI'], $_SERVER['HTTP_REMOTE_ADDR']);
      fwrite($f, $entry);
      fclose($f);
    ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Woah... you've completely lost me there. I understand the whole re-reading the file bit, but the <<< END thing? What's that?
    - Mike

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
  •