码迷,mamicode.com
首页 > Web开发 > 详细

php简单日志记录脚本

时间:2014-06-09 22:50:13      阅读:564      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

自己的站点经常被搜索引擎爬到,想知道搜索引擎和来访者都对哪些内容比较感兴趣,于是写了一个简单的日志记录"系统",来记录来访者的信息。

分为三个文件,record.php,log.php,conn.php

conn.php是数据库连接文件,调用该文件返回一个mysql的数据库读写接口;

log.php是后台,可以从这里看到记录在数据库中的内容;

record.php是记录访客用的脚本,在任意页面包含该脚本,即可实现对于来访者的记录。

代码如下:

conn.php:

bubuko.com,布布扣
<?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);
?>
bubuko.com,布布扣

log.php

bubuko.com,布布扣
<!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);
}

?>
bubuko.com,布布扣

record.php

bubuko.com,布布扣
<?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‘};
}
 ?>
bubuko.com,布布扣

为了方便使用,还写了一个install.php来配置数据库。

install.php:

bubuko.com,布布扣
<?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);

?>
bubuko.com,布布扣

初学php,技术毕竟有限,很多想要的功能还没能加上去。

 

php简单日志记录脚本,布布扣,bubuko.com

php简单日志记录脚本

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/Chorder/p/3774992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!