php网站访问统计代码

2014-03-23 14:08:21

[开发运维 | Dev Ops] , ,

用淘宝的API客户端写了一个统计代码,因为用Google 的统计的话,有时候Google被封了或者访问有问题,国内就会大幅度拖慢网站的访问速度,于是,就自己写了一个。需要服务器支持MySQLi。这是第一次使用MySQLi感觉这个方法比原来的mysql函数好用多了,以后要学习一下更加高级的方法。

 

文件如下有tj.js  , tj.php  ,还有 initial.php

 

 

这个是tj.js 需要网站支持jQuery,本人似乎是有点jQuery依赖症,特别喜欢使用jQuery,记得江url改一下。

$(document).ready(function(){
	$.ajax({
		type: "POST",
		url: "https://masterchan.me/tj.php",
		data: {
			p: window.location.href
		}
	});
});

 

 

这个是tj.php,需要服务器支持curl,连接淘宝网的API获取数据。可能要修改一下initial.php文件的位置。

<?php
	if (isset($_POST['p'])) {

		require('s/initial.php');

		$ip			= $_SERVER['REMOTE_ADDR'];
		$request	= __init_url__.$ip; 

		$data		= json_decode(get_url_content($request)); 

		if ( $data->code == 0 ) {	

			$connection = create_db_connection();

			$useragent	= mysqli_real_escape_string($connection,$_SERVER['HTTP_USER_AGENT']);
			$url		= mysqli_real_escape_string($connection,$_POST['p']);

			$db_name	= __db_name__;
			$table_name	= __db_table__;

			$info 		= $data->data;

			$sql = "INSERT INTO `$db_name`.`$table_name` (`id`, `page`, `ip`, `visittime`, `useragent`, `country`, `area`, `region`, `city`, `isp`, `isp_id`) VALUES (NULL, '$url', '$ip', NOW(), '$useragent', '$info->country','$info->area', '$info->region', '$info->city', '$info->isp', '$info->isp_id');";

			mysqli_query($connection,$sql);

			destory_db_connection($connection);
		}
	}
?>

 

 

initial.php 的文件,这个是我用作很多数据库连接的模版文件,非常好用。

<?php
	define('__db_host__', 			'localhost');			//数据库主机
	define('__db_username__', 		'analytics');			//用户名
	define('__db_password__', 		'xxxxxxxxxxxxxxxx');	//数据库连接密码
	define('__db_name__', 			'analytics');			//数据库名称
	define('__db_table__', 			'masterchan-me');		//表格名称
	define('__db_charset__', 		'utf8');
	define('__db_sort_charset__', 	'utf8_general_ci');
	define('__init_url__', 			'http://ip.taobao.com/service/getIpInfo.php?ip=');

function create_db_connection(){

	$handle = mysqli_connect(__db_host__,__db_username__,__db_password__);
	mysqli_select_db($handle, __db_name__);
	mysqli_query($handle,'SET NAMES '.__db_charset__); 
	mysqli_query($handle,'SET CHARACTER SET '.__db_charset__);
	mysqli_query($handle,'SET COLLATION_CONNECTION=`'.__db_sort_charset__.'`');
	return $handle;

}

function destory_db_connection($handle){

	mysqli_close($handle);

}

function get_url_content($url){

	$thread = curl_init($url);  
    curl_setopt($thread, CURLOPT_HEADER, 0);  
    curl_setopt($thread, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($thread, CURLOPT_CONNECTTIMEOUT, 5);
    $content = curl_exec($thread);  
    curl_close($thread); 

    return $content;
}

?>

 

 

这篇博文发表在 开发运维 | Dev Ops 目录下,标签为 , ,
如需引用,请使用链接:https://note.mc256.dev/?p=293

This article published in 开发运维 | Dev Ops with tags , , .
Cite this page using this link:https://note.mc256.dev/?p=293

您的邮箱地址不会被公开,评论使用Gravatar头像。
Your email address will not be published. This blog is using Gravatar.

正在提交评论... Submitting ...
正在为您准备评论控件 Loading Comment Plugin
Copyright © 2013-2024 mc256. All Rights Reserved.
Powered by WordPress on top of a dual-stack k3s Cluster using JuiceFS.
Wordpress Theme Designed By mc256.
Encrypted By Let's Encrypt.  Hosted On Linode + OVH + AWS.
DNS Provided By Hostker.
Status Page by CloudFlare Worker.