
Log Visitor
PHP memiliki build-in function untuk membaca dan menulis file. Kita akan gunakan function ini untuk membuat catatan/log pengunjung/visitor website kita dengan coding PHP.
Berikut ini Coding PHP selengkapnya.
<?php
error_reporting(0);
date_default_timezone_set("Asia/Jakarta");
main();
function outDir()
{ $outPath = $outPath.'visitorlog\\';
if (!file_exists($outPath))
{ mkdir($outPath,0777) or die("can't make subfolder"); }
return $outPath;
}
function openfile($path,$createDate)
{ $outFile = $path.$createDate.'.html';
if (!file_exists($outFile))
{ $fh = fopen($outFile, 'w') or die("can't open file");
initiateoutput($fh,$createDate);
}
else
{ $fh = fopen($outFile, 'a') or die("can't open file"); }
return $fh;
}
function createoutput($file,$execTime,$url,$ipAddress,$browser)
{ fwrite($file,"<tr><td align='center'>$execTime</td><td>".
strtoupper($url).
"</td><td>$ipAddress</td><td>$browser</td></tr>");
fclose($file);
}
function initiateoutput($fh,$createDate)
{ fwrite($fh,"<html>\n");
fwrite($fh,"<head><title>Laporan</title></head>\n");
fwrite($fh,"<body>\n");
fwrite($fh,"</h1>");
fwrite($fh,"<table border='1' align='center' valign='center'>\n
<caption><h1>Data Pengunjung Website per $createDate</h1>
</caption>\n");
fwrite($fh,"<tr><th width='200' align='center'>Waktu</th>
<th width='200' align='center'>Halaman</th>
<th width='100' align='center'>IP Address</th>
<th width='250' align='center'>Browser</th>\n");
}
function main()
{ $execTime = date('Y-m-d H:i:s');
$createDate = date('d-M-Y');
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$ipAddress = gethostbyname( $hostname);
$browser = $_SERVER['HTTP_USER_AGENT'];
$path = outDir();
$file = openfile($path,$createDate);
createoutput($file,$execTime,$url,$ipAddress,$browser);
$file = openfile($path,$createDate);
include "$path"."$createDate".".html";
}
?>
Silakan Anda pelajari setiap baris dari coding tersebut.
Selamat mencoba, semoga berhasil.