1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP File to Log Visitors

Discussion in 'PHP & Perl' started by illmill, Mar 20, 2013.

  1. illmill

    illmill Registered Member

    Joined:
    Jun 10, 2009
    Messages:
    69
    Likes Received:
    35
    Home Page:
    Hi guys, I basically want to upload a PHP script to my server that will give me this info for every single person/bot that lands on that page:
    user agent, IP address, and referrer.

    Any suggestions??
     
  2. nesterdwarf

    nesterdwarf Regular Member

    Joined:
    May 30, 2008
    Messages:
    276
    Likes Received:
    539
    Location:
    State of Misery...er, Missouri...
    This should get you started at least -

    Code:
    
    <?php
    $myFile = "testFile.txt";
    $ip = $_SERVER['REMOTE_ADDR'];
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $ref = $_SERVER['HTTP_REFERER'];
    $stringData = $agent . " - " . $ip . " - " . $ref . "\n";
    $fh = fopen($myFile, 'a') or die("can't open file");
    fwrite($fh, $stringData);
    fclose($fh);
    ?>
    
    
    Output will look something like this -

    Code:
    Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0 - 174.125.36.177 - 
    
    Note that the $ref variable will be empty if you go to the page directly, so there should be some information behind the last "-".

    HTH
    ND
     
  3. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,288
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    thats a good and simple solution, however i would advice you to use file_put_contents() and place a LOCK on the file. Depending on the site traffic it might loose some writes in your solution if there are much simultaneous visitors..
     
  4. 0_00_0

    0_00_0 Senior Member

    Joined:
    Oct 7, 2010
    Messages:
    1,024
    Likes Received:
    486
    Location:
    Canada
    I would advise using a database (SQL) instead of a text file. Just slightly modify the code given above. Beware this file can get pretty big if your website gets a lot of traffic.
     
  5. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,288
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    yeah, db is always better, but the guy probably wants just a text file..
     
  6. RingoM

    RingoM Newbie

    Joined:
    Mar 13, 2013
    Messages:
    20
    Likes Received:
    2
    If you don't need to import that information into anything, then your server will probably be logging this information anyway - most hosts offer some kind of webstats program like Awstats or even the option to download raw access log files.
     
  7. inviz

    inviz Newbie

    Joined:
    Jun 15, 2010
    Messages:
    45
    Likes Received:
    5
    add google analytics to your site! :)
     
  8. illmill

    illmill Registered Member

    Joined:
    Jun 10, 2009
    Messages:
    69
    Likes Received:
    35
    Home Page:
    This is a horrible suggestion! Sorry, but I'm not looking for newb advice.

    Thanks for all the other info on this thread, though, I think that script will work just fine for my needs. Yeah, db is better, but I'm just using this to check the hits I'm getting and then shutting off traffic after maybe 100 visitors. I might log more visitors later, but just wanted something simple like this.
     
    Last edited: Mar 22, 2013
  9. inviz

    inviz Newbie

    Joined:
    Jun 15, 2010
    Messages:
    45
    Likes Received:
    5
    Newb advice?

    Google analytics is problaby one of the most detailed and best solutions out there to keep track of your visitors. There is also awstats wich could easily be installed on your server, if you have control over it ofcourse.


    why reinvent the weel? Why waste time programming something thats already made and as good as it could get when it comes to stats?

    It's totally upto you, but I don't think I'm the "newb" here if you actually have to ask how to make your own statistics script.. I could make that with a blindfold covering my eyes.

    Good luck
     
    Last edited: Mar 29, 2013
  10. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    It 's also a Google product and you don't want it on your BH sites, do you? ;)
     
  11. illmill

    illmill Registered Member

    Joined:
    Jun 10, 2009
    Messages:
    69
    Likes Received:
    35
    Home Page:
    Why am I even wasting my time on this issue? Like I said, you're clearly a newb. First of all, I don't want google knowing shit about my site. Secondly, google analytics doesn't give you numbers on everyone who visits your site. I want to know every bot or spider that crawls my site as well as people who don't have javascript. Many people use javascript blockers so those people won't be reported on your site.

    Seriously, I'm trying to help you out here, if you want an alternative to Google Analytics that's self-hosted and free, check out Piwik. Google Analytics is about the worst analytics program you can use on your sites, ESPECIALLY if you're using blackhat methods.