phploggingvisitor-statistic

help debug a php page


<?php
error_reporting(E_ALL);
// Getting the information 
$ipaddress = $_SERVER['REMOTE_ADDR']; 
$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
if(!empty($_SERVER['QUERY_STRING']) $page .= $_SERVER['QUERY_STRING'];
$referrer = $_SERVER['HTTP_REFERER'];
$datetime = mktime(); 
$useragent = $_SERVER['HTTP_USER_AGENT'];
$remotehost = getHostByAddr($ipaddress);

// Create log line
$logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . "\n"; 
echo $logline;
// Write to log file: 
//$logfile = 'logfile.txt';
$logfile = '/home/www/agro-dive.onlinewebshop.net/logfile.txt';

// Open the log file in "Append" mode 
if (!$handle = fopen($logfile, 'a+')) {
    die("Failed to open log file"); 
} 

// Write $logline to our logfile. 
if (fwrite($handle, $logline) == FALSE) {
    die("Failed to write to log file"); 
} 

fclose($handle);  

?>

if i try to open this php it gives me a server error

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

and i have also tested it follow the steps here, but it didn't produce any log message


Solution

  • I can't answer your question about debugging and logging but for what it's worth. Your error is on line 6. Replace it by following:

    if(!empty($_SERVER['QUERY_STRING']))
        $page .= $_SERVER['QUERY_STRING'];