Reputation: 24
I am working with a project and i made a code to see who ais visiting my site But i am facing a problam The code works well but it collects all the information in one file Now i just to make day by day log I mean every day a new file will be created and it automatically log in that file After one day a new file will be created
Here is my code:
<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// Getting OS Name
function getOS() {
global $user_agent;
$os_platform = "Unknown OS Platform";
$os_array = array(
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/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',
'/kalilinux/i' => 'KaliLinux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile',
'/Windows Phone/i' => 'Windows Phone'
);
foreach ($os_array as $regex => $value) {
if (preg_match($regex, $user_agent)) {
$os_platform = $value;
}
}
return $os_platform;
}
// END of Getting OS
//
// Get browser
function getBrowser() {
global $user_agent;
$browser = "Unknown Browser";
$browser_array = array(
'/msie/i' => 'Internet Explorer',
'/firefox/i' => 'Firefox',
'/Mozilla/i' => 'Mozila',
'/Mozilla/5.0/i'=> 'Mozila',
'/safari/i' => 'Safari',
'/chrome/i' => 'Chrome',
'/edge/i' => 'Edge',
'/opera/i' => 'Opera',
'/OPR/i' => 'Opera',
'/netscape/i' => 'Netscape',
'/maxthon/i' => 'Maxthon',
'/konqueror/i' => 'Konqueror',
'/Bot/i' => 'BOT Browser',
'/Valve Steam GameOverlay/i' => 'Steam',
'/Googlebot/i' => 'GOOGLE Bot',
'/OrbitFox/i' => 'Orbit Fox Bot',
'/mobile/i' => 'Handheld Browser'
);
foreach ($browser_array as $regex => $value) {
if (preg_match($regex, $user_agent)) {
$browser = $value;
}
}
return $browser;
}
// END of getting browser
$user_os = getOS();
$user_browser = getBrowser();
// Comming soon part --- Maybe :D
// Getting visitor IP address
$ip = $_SERVER['REMOTE_ADDR'];
// Getting where visitor come
// Hide ownr's IP address
$owner = "lol"; //Change $owner for something else, cuz someone can simple read that by calling out $owner
// change it for $absdfs5sd4 for example and change it down there
$owner_country = "YOUR COUNTRY TAG FOR YOUR IP ↑"; //This u can leave how it is.
if($ip == $owner){ //Change it here
$ip = "Owner";
$country = $owner_country;
}
//If that wasn't you, it woun't change IP address and it will find info about IP address
else{
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
$country = $details->country;
}
$dataTime = date_default_timezone_set("Asia/Dhaka");
$dataTime = date_default_timezone_get();
$dateTime = date('D M d, Y h:i:s a');
$file = "/home/shakilofficial/public_html/vtinfo/mainindex.html";
$file = fopen($file, "a");
$data = "<p>##################</p><br><p><b>User Time </b>: $dateTime </p><br><pre> <b> User IP </b>: $ip <b> Browser</b>: $user_browser <br> <b> User OS </b>: $user_os <b> Users-From </b>: $country <br><br><b> User-agent </b>: $user_agent </pre>";
fwrite($file, $data);
fclose($file);
?>
<html>
<body>
<h1>Its Okh</h1>
</body>
</html>
Upvotes: 0
Views: 537
Reputation: 65274
The easiest way would be to replace the line
$file = "/home/shakilofficial/public_html/vtinfo/mainindex.html";
with something along the lines of
$file = "/home/shakilofficial/public_html/vtinfo/mainindex-".date('Y-m-d').".html";
This will put all request from one date into an aptly named file.
Upvotes: 1
Reputation: 760
Crate one more variable like the $date one:
$newTime = date('dmY');
And then add this in your file name:
$file = "/home/shakilofficial/public_html/vtinfo/mainindex{$newTime}.html";
So you will have a different file every day with different name
Upvotes: 1