Jump to content

Two logs. One loging visitors and another one loging search engines.


strago

Recommended Posts

How do you have a log generate two different logs, and having where it posts the log at, depend on if it's a visitor, or a search engine spider HTTP_USER_AGENT (Like Googlebot, Msnbot, Yahoo! Slurp.)

 

<?php
    define("DATE_FORMAT","m-d-Y - H:i:s");
    define("LOG_FILE","/full_path/visitors.html");
    define("LOG_FILE2","/full_path/search_engine_bots.html");

    $logfileHeader='DATE - IP - HOSTNAME - BROWSER - URI - REFERRER'."\n";

    $userAgent = (isset($_SERVER['HTTP_USER_AGENT']) && ($_SERVER['HTTP_USER_AGENT'] != "")) ? $_SERVER['HTTP_USER_AGENT'] : "Unknown";
    $userIp    = (isset($_SERVER['REMOTE_ADDR'])     && ($_SERVER['REMOTE_ADDR'] != ""))     ? $_SERVER['REMOTE_ADDR']     : "Unknown";
    $refferer  = (isset($_SERVER['HTTP_REFERER'])    && ($_SERVER['HTTP_REFERER'] != ""))    ? $_SERVER['HTTP_REFERER']    : "Unknown";
    $uri       = (isset($_SERVER['REQUEST_URI'])     && ($_SERVER['REQUEST_URI'] != ""))     ? $_SERVER['REQUEST_URI']     : "Unknown";

    $hostName   = gethostbyaddr($userIp);
    $actualTime = date(DATE_FORMAT);

    $logEntry = "$actualTime - $userIp - $hostName - $userAgent - $uri - $refferer<BR>\n";

    if (!file_exists(LOG_FILE)) {
        $logFile = fopen(LOG_FILE,"w");
        fwrite($logFile, $logfileHeader);
    }
    else {
        $logFile = fopen(LOG_FILE,"a");
    }

    fwrite($logFile,$logEntry);
    fclose($logFile);
?>

Link to comment
Share on other sites

For a search engine only log, would I only need to change

 

($_SERVER['HTTP_USER_AGENT'] != ""))

 

to

 

($_SERVER['HTTP_USER_AGENT'] != "Googlebot|MantraAgent|Slurp|WebCrawler|ia_archiver|FAST-WebCrawler"))

 

and add an else for making a log that doesn't contain those?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.