首页 > 代码库 > php简单日志记录脚本
php简单日志记录脚本
自己的站点经常被搜索引擎爬到,想知道搜索引擎和来访者都对哪些内容比较感兴趣,于是写了一个简单的日志记录"系统",来记录来访者的信息。
分为三个文件,record.php,log.php,conn.php
conn.php是数据库连接文件,调用该文件返回一个mysql的数据库读写接口;
log.php是后台,可以从这里看到记录在数据库中的内容;
record.php是记录访客用的脚本,在任意页面包含该脚本,即可实现对于来访者的记录。
代码如下:
conn.php:
<?php #conn.php #include this file to connect the database which you choose #while database was connected,it will return a connect hundle "$con" #then you could use this hundle to excute sql. $dbhost = "localhost";#the database host(default localhost) $dbuser = "xxx";#name of database user $dbpswd = "xxx";#password of database user $dbname = "xxx";#the tablename you want to save log $con=mysql_connect($dbhost,$dbuser,$dbpswd); if (!$con) { die(‘Could not connect: ‘.mysql_error()); } mysql_select_db($dbname, $con); ?>
log.php
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <meta http-equiv="Cache-Control" content="max-age=0" forua="true"/> <meta http-equiv="Cache-Control" content="no-cache"/> <meta http-equiv="Expires" content="0"/> <title>#</title> </head> <body> <hr /> </body> </html> <?php showlog(); function showlog() { include "conn.php"; #mysql_query(‘set names gb2312;‘); $query="select * from record"; $result=mysql_query($query); while($rows=mysql_fetch_array($result)) { echo "<b>".$rows[‘id‘]."</b> "; echo "<b>".$rows[‘ip‘]." ".$rows[‘area‘]."</b><br /> "; echo $rows[‘request‘]."<br />"; if($rows[‘source‘]!="") { echo "From:".$rows[‘source‘]."<br />"; } echo $rows[‘useragent‘]."<br />"; echo $rows[‘userlang‘]."<br />"; echo "<b>".$rows[‘time‘]."</b>"; echo "<hr />"; } mysql_free_result($result); } ?>
record.php
<?php error_reporting(0); date_default_timezone_set(‘PRC‘); $ip = $_SERVER[‘REMOTE_ADDR‘]; $time = date(‘Y-m-d H:i:s‘); $source = $_SERVER [‘HTTP_REFERER‘]; $user_agent = $_SERVER [‘HTTP_USER_AGENT‘]; $user_lang = $_SERVER [‘HTTP_ACCEPT_LANGUAGE‘]; $request = $_SERVER[‘SERVER_NAME‘].$_SERVER["REQUEST_URI"]; $area = ip_lookup(); include "conn.php"; $sql="INSERT INTO `record` (`ip` ,`source`,`request`,`useragent`,`userlang`,`area`,`time`) VALUES (‘$ip‘,‘$source‘,‘$request‘,‘$user_agent‘,‘$user_lang‘,‘$area‘,‘$time‘);"; mysql_query($sql,$con); mysql_close($con); #ip_lookup函数通过sina的ip查询接口用来获取ip地址对应的地区 function ip_lookup() { $ip = $_SERVER[‘REMOTE_ADDR‘]; $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip; $json = file_get_contents($url); $json = json_decode($json); return $json->{‘country‘}.$json->{‘province‘}.$json->{‘city‘}." ".$json->{‘isp‘}; } ?>
为了方便使用,还写了一个install.php来配置数据库。
install.php:
<?php include "conn.php"; $sql ="CREATE TABLE `record` ( `id` INT(5) NOT NULL PRIMARY KEY AUTO_INCREMENT, `ip` TEXT NOT NULL , `source` TEXT NOT NULL , `request` TEXT NOT NULL, `useragent` TEXT NOT NULL, `userlang` TEXT NOT NULL, `area` TEXT NOT NULL, `time` DATETIME NOT NULL ) ENGINE = MYISAM "; if(mysql_query($sql,$con)) { echo "Install Success!"; } else { die(‘Could not connect: ‘.mysql_error()); } mysql_close($con); ?>
初学php,技术毕竟有限,很多想要的功能还没能加上去。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。