PHP Simple Log Logging script

Source: Internet
Author: User

Their site is often crawled by search engines, want to know what search engines and visitors are interested in what content, so wrote a simple logging "system" to record the visitor's information.

Divided into three files, record.php,log.php,conn.php

conn.php is a database connection file that calls the file to return a MySQL database read-write interface;

Log.php is the background, you can see the contents of the record in the database;

Record.php is a script that records visitors, and the script is included on any page to enable a record of the visitor.

The code is as follows:

conn.php:

<?PHP#conn.php#include this file to connect the database which you choose#While database was connected,it would return a connect Hundle "$con"#Then you could the 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 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" >#</title>PHPShowlog ();functionShowlog () {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"; }    Mysql_free_result($result);}?>

record.php

<?PHPerror_reporting(0);d Ate_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);#The ip_lookup function uses the IP lookup interface of Sina to obtain the corresponding region of the IP address.functionIp_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 '};} ?>

For ease of use, a install.php was written to configure the database.

install.php:

<?PHPinclude"Conn.php";$sql="CREATE TABLE ' record ' (' ID ' INT (5) NOT null PRIMARY KEY auto_increment, ' IP ' text is not null, ' source ' text is not null, ' re Quest ' Text NOT null, ' useragent ' text is not null, ' Userlang ' text is not null, ' area ' text is not null, ' time ' DATETIME is not null) ENG INE = MYISAM";if(mysql_query($sql,$con)){    Echo"Install success!";}Else{     die(' Could not connect: '.Mysql_error());}Mysql_close($con);?>

Beginner PHP, the technology is limited after all, a lot of features have not been able to add.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.