您的位置 首页 php

PHP记录用户访问ip、浏览器类型和访问时间到txt日志文件

<?php

/*

* 日志记录

* 每天生成一个日志文件,当文件超过指定大小则备份日志文件并重新生成新的日志文件

*

*/

class Log {

private $maxsize = 1024000; //最大文件大小1M

//写入日志

public function writeLog($filename,$ msg ){

$res = array();

$res[‘msg’] = $msg;

$res[‘logtime’] = date(“Y-m-d H:i:s”,time());

//如果日志文件超过了指定大小则备份日志文件

if(file_exists($filename) && (abs(filesize($filename)) > $this->maxsize)){

$newfilename = dirname($filename).’/’.time().’-‘.basename($filename);

rename ($filename, $newfilename);

}

//如果是新建的日志文件,去掉内容中的第一个字符逗号

if(file_exists($filename) && abs(filesize($filename))>0){

$content = “,”. json _encode($res);

}else{

$content = json_encode($res);

}

//往日志文件内容后面追加日志内容

file_put_contents($filename, $content, FILE_APPEND);

}

//读取日志

public function readLog($filename){

if(file_exists($filename)){

$content = file_get_contents($filename);

$json = json_decode(‘[‘.$content.’]’,true);

}else{

$json = ‘{“msg”:”The file does not exist.”}’;

}

return $json;

}

}

?>

index.php

<?php

ini_set(‘date.timezone’,’Asia/Shanghai’);

require_once(‘Log.class.php’);

$filename = “logs/log_” . date(“Ymd”, time()) . “.txt”;

$msg = array(

‘ip’ => $_SERVER[“REMOTE_ADDR”],

‘user’ => getBrowser()

);

$Log = new Log();

$Log->writeLog($filename, $msg);

$loglist = $Log->readLog($filename);

//获取浏览器类型

function getBrowser() {

$user_OSagent = $_SERVER[‘HTTP_USER_AGENT’];

if (strpos($user_OSagent, “Maxthon”) && strpos($user_OSagent, “MSIE”)) {

$visitor_browser = “Maxthon(Microsoft IE)”;

} elseif (strpos($user_OSagent, “Maxthon 2.0”)) {

$visitor_browser = “Maxthon 2.0”;

} elseif (strpos($user_OSagent, “Maxthon”)) {

$visitor_browser = “Maxthon”;

} elseif (strpos($user_OSagent, “Edge”)) {

$visitor_browser = “Edge”;

} elseif (strpos($user_OSagent, “Trident”)) {

$visitor_browser = “IE”;

} elseif (strpos($user_OSagent, “MSIE”)) {

$visitor_browser = “IE”;

} elseif (strpos($user_OSagent, “MSIE”)) {

$visitor_browser = “MSIE 较高版本”;

} elseif (strpos($user_OSagent, “NetCaptor”)) {

$visitor_browser = “NetCaptor”;

} elseif (strpos($user_OSagent, “Netscape”)) {

$visitor_browser = “Netscape”;

} elseif (strpos($user_OSagent, “Chrome”)) {

$visitor_browser = “Chrome”;

} elseif (strpos($user_OSagent, “Lynx”)) {

$visitor_browser = “Lynx”;

} elseif (strpos($user_OSagent, “Opera”)) {

$visitor_browser = “Opera”;

} elseif (strpos($user_OSagent, “MicroMessenger”)) {

$visitor_browser = “微信浏览器”;

} elseif (strpos($user_OSagent, “Konqueror”)) {

$visitor_browser = “Konqueror”;

} elseif (strpos($user_OSagent, “Mozilla/5.0”)) {

$visitor_browser = “Mozilla”;

} elseif (strpos($user_OSagent, “Firefox”)) {

$visitor_browser = “Firefox”;

} elseif (strpos($user_OSagent, “U”)) {

$visitor_browser = “Firefox”;

} else {

$visitor_browser = “其它”;

}

return $visitor_browser;

}

?>

文章来源:智云一二三科技

文章标题:PHP记录用户访问ip、浏览器类型和访问时间到txt日志文件

文章地址:https://www.zhihuclub.com/79093.shtml

关于作者: 智云科技

热门文章

评论已关闭

31条评论

  1. Ku is a heterodimer that encircles broken DNA ends during repair and can recruit the DNA PK catalytic subunit DNA PKcs 134

  2. 162, 163 The advantages of this model were clear as periodontal disease could be noted as early as 16 days of age, and destructive pathological changes in the bone within 90 100 days, much faster than the years it takes to develop most types of periodontal disease in humans

  3. It s been this way since I got chemo It turns out that cancer care providers are not adequately screening female survivors for these issues, and patients might be afraid to ask

  4. 6049 Renal organic cation transporter Non inhibitor 0 In a particular embodiment, a subject receives one or more 2 10 or more treatments with a 1550 nm fraction Er Glass Laser at 2 week intervals using a 5 10 mm tip, 6 mJ pulse energy, 800 spot cm 2 density, and static mode, as described in Lee et al

  5. abana costo renagel 800 Beaumont is not alone in taking action against cold calls Гў in October 2012 Richard Herman told how he had won cash from a claims management company to compensate him for wasting his time with calls 1021 jm970873c Search in Google Scholar PubMed

  6. Given that 95 of PDAC is KRAS driven and that KRAS plays an important role in scavenging nutrients 52, 53, elevated plasma levels of BCAAs may indicate a prominent role of protein breakdown in KRAS mutant PDAC I also take milk thistle, flax seed and extra vit C and magnesium, and make my own burdock sheepsorrel tea

  7. All the topical ointments were enclosed in identical sealed covers, were computer coded and allotted by the second clinician having the key for the treatment options since it will shut you down

  8. As an example the numerator could be live birth and the denominator could be egg retrievals Among the tested antibiotics, tobramycin and amikacin showed synergy for most of the strains in maximum number of fractional doses

  9. Therefore, the Secretary has determined that this proposed rule would not have a significant impact on the operations of a substantial number of small rural hospitals

  10. aleve for muscle spasms We conduct these strikes because they are necessary to mitigate ongoing, actual threats, to stop plots, to prevent further attacks and, again, to save American lives, White House Press Secretary Jay Carney said then

  11. The main outcome measures were restoration of blood pressure with adequate organ perfusion and survival at discharge

  12. While the same study noted above shows that the age of onset for menopause in IVF patients was earlier, the difference between those who had undergone IVF and those who had not isn t clinically significant and requires more research Negishi T, Thavendiranathan P, Negishi K, Marwick TH

  13. Thrombophlebitis can occur at the site of injection, and anaphylactic shock has been reported following parenteral administration of tetracycline to a dog Testicular shrinkage comes with the gear man

  14. The popular CAH therapy with glucocorticoids usually protects the testes and resumes sperm production

网站地图