簡體   English   中英

每天通過php和Javascript創建日志文件

[英]Creating A log File every day By php and Javascript

我正在一個項目中,我編寫了一個代碼來查看誰在訪問我的網站,但是我面臨一個問題。代碼運行良好,但是它將所有信息收集到一個文件中。現在,我只是每天做一次日志,意思是每天將創建一個新文件,並自動登錄該文件。一天后,將創建一個新文件。

這是我的代碼:

<?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>

$ date一一樣創建一個變量:

$newTime = date('dmY');

然后將其添加到您的文件名中:

$file = "/home/shakilofficial/public_html/vtinfo/mainindex{$newTime}.html";

因此,您每天都會使用不同的名稱創建一個不同的文件

最簡單的方法是更換生產線

$file = "/home/shakilofficial/public_html/vtinfo/mainindex.html";

與類似的東西

$file = "/home/shakilofficial/public_html/vtinfo/mainindex-".date('Y-m-d').".html";

這會將所有請求從一個日期起放入一個適當命名的文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM