Skip to content

Commit

Permalink
Init File
Browse files Browse the repository at this point in the history
  • Loading branch information
muhaiminmuh committed Feb 4, 2019
1 parent 29f151b commit a41ff87
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# web-statistik


Web Statistik - Merekam Informasi Pengunjung Website

Program sederhana yang digunakan untuk merekam pengunjung website, kemudian tersimpan pada database.
Informasi yang terekam meliputi IP, Browser, OS, Timezone, Waktu Akses, DLL.

## Special Thanks

Program ini menggunakan API dari <a href="http:https://ip-api.com/">IP-API</a>

37 changes: 37 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
require 'config.php';

$sql = "SELECT * FROM statistik_api ORDER BY date_create DESC";
$query = $db->query($sql);

?>
<h3>Daftar user yang pernah mengakses website</h3>
<table border="1">
<tr>
<td>IP</td>
<td>Browser</td>
<td>OS</td>
<td>ISP</td>
<td>Country</td>
<td>Region</td>
<td>City</td>
<td>Latitude, Longitude</td>
<td>Timezone</td>
<td>Date</td>
</tr>
<?php
while ($row=$query->fetch_assoc()) { ?>
<tr>
<td><?php echo $row['ip'];?></td>
<td><?php echo $row['browser'];?></td>
<td><?php echo $row['os'];?></td>
<td><?php echo $row['isp'];?></td>
<td><?php echo $row['country'];?></td>
<td><?php echo $row['region'];?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['latitude'] ." , ". $row['longitude'];?></td>
<td><?php echo $row['timezone'];?></td>
<td><?php echo $row['date_create'];?></td>
</tr>
<?php } ?>
</table>
Binary file added capture/hasil.JPG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Informasi akun untuk akses database
* @var $dbhost host database biasanya diisi 'localhost' atau '127.0.0.1'
* @var $dbuser username database, untuk xampp biasanya diisi "root"
* @var $dbpass password database, untuk xampp biasanya dikosongkan
* @var $dbname nama database yang akan diakses
*/
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'statistik_api';

// Fungsi untuk koneksi ke database { menggunakan MySQLi Object Oriented }
$db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);

// Check hasil fungsi koneksi berhasil atau tidak

// Jika terjadi Error
if ($db->connect_error) {

// Sistem dihentikan
die('Terjadi Kegagalan : '. $db->connect_error );
}
?>
143 changes: 143 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* @return IP (192.168.1.1)
*/
function ip_user()
{
if (! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ip = $_SERVER['HTTP_CLIENT_IP'];

} elseif (! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

} else {
$ip = $_SERVER['REMOTE_ADDR'];

}

return $ip;
}

/**
* @see http:https://php.net/manual/en/function.get-browser.php;
* @return
*/
function browser_user()
{
$browser = _userAgent();
return $browser['name'] . ' v.'.$browser['version'];
}

/**
* Deteksi UserAgent / Browser yang digunakan
* @return [type] [description]
*/
function _userAgent()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version = "";

$os_array = array(
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);

foreach ($os_array as $regex => $value) {

if (preg_match($regex, $u_agent)) {
$platform = $value;
break;
}

}

// Next get the name of the useragent yes seperately and for good reason
if (preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";

} elseif(preg_match('/Firefox/i',$u_agent)) {
$bname = 'Mozilla Firefox';
$ub = "Firefox";

} elseif(preg_match('/Chrome/i',$u_agent)) {
$bname = 'Google Chrome';
$ub = "Chrome";

} elseif (preg_match('/Safari/i',$u_agent)) {
$bname = 'Apple Safari';
$ub = "Safari";

} elseif (preg_match('/Opera/i',$u_agent)) {
$bname = 'Opera';
$ub = "Opera";

} elseif (preg_match('/Netscape/i',$u_agent)) {
$bname = 'Netscape';
$ub = "Netscape";
}

// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';

if (! preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}

// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];

} else {
$version= $matches['version'][1];
}
} else {
$version= $matches['version'][0];
}

// check if we have a number
$version = ( $version == null || $version == "" ) ? "?" : $version;

return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}

/**
* @return name Operating System*/
function os_user()
{
$OS = _userAgent();
return $OS['platform'];
}
?>
53 changes: 53 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

require 'config.php';
require 'functions.php';

/**
* Test
* echo ip_user();
* echo "<br/>";
* echo browser_user();
* echo "<br/>";
* echo os_user();
*/

// GET data user yang sudah mengakses website kita
$ip = ip_user();
$browser = browser_user();
$os = os_user();

// USE API TO GET VISITOR DATA
$queryapi=@unserialize(file_get_contents('http:https://ip-api.com/php/'.$ip));


if($queryapi && $queryapi['status'] == 'success') {

$isp = $queryapi['isp'];
$country = $queryapi['country'];
$region = $queryapi['regionName'];
$city = $queryapi['city'];
$latitude = $queryapi['lat'];
$longitude = $queryapi['lon'];
$timezone = $queryapi['timezone'];

// Check bila sebelumnya data pengunjung sudah terekam
if (! isset($_COOKIE['VISITOR'])) {

// Cookie disimpan selama 24 jam
$duration = time()+60*60*24;

// simpan kedalam cookie browser
setcookie('VISITOR',$browser,$duration);

// SQL Command atau perintah SQL INSERT
$sql = "INSERT INTO statistik_api (ip, os, browser, isp, country, region, city, latitude, longitude, timezone) VALUES ('$ip', '$os', '$browser', '$isp', '$country', '$region', '$city', '$latitude', '$longitude', '$timezone')";

$query = $db->query($sql);

}
}

echo "<p>Untuk Melihat siapa saja yang telah mengakses website ini, silahkan buka pada <a href=\"admin.php\"/>Halaman Admin</a></p>";
?>

78 changes: 78 additions & 0 deletions statistik_api.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Feb 01, 2019 at 04:57 AM
-- Server version: 10.1.25-MariaDB
-- PHP Version: 5.6.31

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `statistik_api`
--

-- --------------------------------------------------------

--
-- Table structure for table `statistik_api`
--

CREATE TABLE `statistik_api` (
`id_statistik` int(11) NOT NULL,
`ip` varchar(20) NOT NULL,
`os` varchar(30) NOT NULL,
`browser` varchar(130) NOT NULL,
`isp` varchar(40) NOT NULL,
`country` varchar(40) NOT NULL,
`region` varchar(40) NOT NULL,
`city` varchar(40) NOT NULL,
`latitude` double NOT NULL,
`longitude` double NOT NULL,
`timezone` text NOT NULL,
`date_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `statistik_api`
--

INSERT INTO `statistik_api` (`id_statistik`, `ip`, `os`, `browser`, `isp`, `country`, `region`, `city`, `latitude`, `longitude`, `timezone`, `date_create`) VALUES
(4, '159.253.145.183', 'test', 'test', 'SoftLayer', 'Netherlands', 'North Holland', 'Amsterdam', 52.3556, 4.9135, 'Europe/Amsterdam', '2019-01-30 16:03:54'),
(5, '112.215.36.145', 'Linux', 'Google Chrome v.60.0.3112.116', 'WestHost, Inc.', 'Indonesia', 'Jakarta', 'Rw 01', -6.2262401580811, 106.82499694824, 'Asia/Jakarta', '2019-02-01 03:54:59'),
(6, '23.235.227.109', 'Unknown', 'Mozilla Firefox v.47.0', 'Secured Servers LLC', 'United States', 'Arizona', 'Tempe', 33.421501159668, -111.97399902344, 'America/Phoenix', '2019-02-01 03:56:03'),
(7, '209.95.56.51', 'Unknown', 'Google Chrome v.71.0.3578.98', 'WestHost, Inc.', 'United States', 'Illinois', 'Chicago', 41.878101348877, -87.62979888916, 'America/Chicago', '2019-02-01 03:56:58');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `statistik_api`
--
ALTER TABLE `statistik_api`
ADD PRIMARY KEY (`id_statistik`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `statistik_api`
--
ALTER TABLE `statistik_api`
MODIFY `id_statistik` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 comments on commit a41ff87

Please sign in to comment.