Script PHP-MySQL menanampilkan Jumlah Pengunjung dengan IP Address
Script PHP-MySQL menanampilkan Jumlah Pengunjung dengan IP Address
Buat struktur database untuk menyimpan data IP Addres penggunjung.
nama database "testip" nama tabel "counter" atau lihat sql berikut.
CREATE TABLE IF NOT EXISTS `counter` (
`id` int(250) NOT NULL AUTO_INCREMENT,
`location` varchar(250) NOT NULL,
`ip` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
Selanjutnya buat file phpnya "index.php"
<?php
// setting koneksi ke database
mysql_connect("localhost", "root", ""); // isikan nama server,username,password.
mysql_select_db("testip"); // nama database yang digunakan.
function initCounter() {
$ip = $_SERVER['REMOTE_ADDR']; // menangkap ip pengunjung
$location = $_SERVER['PHP_SELF']; // menangkap lokasi server path
//membuat log dalam tabel database 'counter'
$create_log = mysql_query("INSERT INTO counter(ip,location)VALUES('$ip', '$location') ");
}
function getCounter($mode, $location = NULL) {
if(is_null($location)) {
$location = $_SERVER['PHP_SELF'];
}
if($mode == "unique") {
$get_res = mysql_query("SELECT DISTINCT ip FROM counter WHERE location = '$location' ");
} else{
$get_res = mysql_query("SELECT ip FROM counter WHERE location = '$location' ");
}
$res = mysql_num_rows($get_res);
return $res;
}
?>
terakhir jalankan file index.php.
Selesai..
Join the conversation