我正在处理一个项目,并且编写了代码以查看谁访问了我的网站 但是我正面临一个问题 该代码运行良好,但是将所有信息收集到一个文件中 现在我只是每天做日志 我的意思是每天都会创建一个新文件,并自动登录该文件 一天后,将创建一个新文件
这是我的代码:
<?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>
答案 0 :(得分:1)
再创建一个变量,例如 $ date 之一:
$newTime = date('dmY');
然后将其添加到您的文件名中:
$file = "/home/shakilofficial/public_html/vtinfo/mainindex{$newTime}.html";
因此,您每天都将使用不同的名称创建一个不同的文件
答案 1 :(得分:1)
最简单的方法是替换行
$file = "/home/shakilofficial/public_html/vtinfo/mainindex.html";
带有类似的内容
$file = "/home/shakilofficial/public_html/vtinfo/mainindex-".date('Y-m-d').".html";
这会将一个日期的所有请求放入一个恰当命名的文件中。