Skip to content

Instantly share code, notes, and snippets.

@plonknimbuzz
Last active April 20, 2019 19:05
Show Gist options
  • Save plonknimbuzz/869d62ed3216af979cd3fb6ab8f5a043 to your computer and use it in GitHub Desktop.
Save plonknimbuzz/869d62ed3216af979cd3fb6ab8f5a043 to your computer and use it in GitHub Desktop.
example traffic lesson
<?php
$url = 'http://localhost/index.php'; //change me
$user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_USERAGENT,$user_agent);
$result=curl_exec($ch);
curl_close($ch);
create table log_view(
id int not null primary key auto_increment,
ipaddress varchar(30) not null,
user_agent varchar(255) not null,
log_created timestamp not null default current_timestamp
)
<?php
//for example only
//care for sql injection issue for real use
$count=0;
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) die('db connection error');
//insert data pengunjung
$ipaddress = getUserIP();
$user_agent = isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'';
$mysqli->query("INSERT INTO log_view (ipaddress,user_agent) VALUES ($ipaddress, $user_agent) ");
//ambil jumlah yg pernah lihat
$result = $mysqli->query("SELECT COUNT(*) AS jml FROM log_view");
if($result){
$row = $result->fetch_assoc();
$count = $row['jml'];
}
//https://stackoverflow.com/a/13646735/3396168
function getUserIP()
{
// Get real visitor IP behind CloudFlare network
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
?><html>
<head></head>
<body>
dilihat <?=$count;?> kali
</body>
</html>
<?php
//for example only
//care for sql injection issue for real use
$count=0;
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) die('db connection error');
//insert data pengunjung
$ipaddress = getUserIP();
$user_agent = isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'';
$result = $mysqli->query("SELECT COUNT(*) AS jml FROM log_view WHERE ipaddress='$ipaddress' AND user_agent ='$user_agent' AND log_created >= $last ");
$row = $result->fetch_assoc();
if($row['jml']==0){
$mysqli->query("INSERT INTO log_view (ipaddress,user_agent) VALUES ($ipaddress, $user_agent) ");
//ambil jumlah yg pernah lihat
$result = $mysqli->query("SELECT COUNT(*) AS jml FROM log_view");
if($result){
$row = $result->fetch_assoc();
$count = $row['jml'];
}
}
//https://stackoverflow.com/a/13646735/3396168
function getUserIP()
{
// Get real visitor IP behind CloudFlare network
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
?><html>
<head></head>
<body>
dilihat <?=$count;?> kali
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment