PHP 简易网页访问统计
<p>传统的网页访问统计,已经有很多,如 51la、百度统计、站长统计</p><p>一般都需要引用JS,在你的网页内嵌入JS,这个操作存在风险,并且不可控。</p>
<p>可以考虑使用 【img src、css src、link href】引用统计;</p>
<p><strong>使用方法:</strong></p>
<p>在需要统计页面添加 <img src="/stat.php" /></p>
<p>访问记录按照站点每日存储,记录内容:time、ip、url、user_agent、统计参数(stat.php?xxxx)</p>
<p>使用 stat.php?null=1 禁止输出svg</p>
<p>使用 stat.php?host=xxx 将多个不同站点统计到一起</p>
<p>后期基于记录内容可以分析访问数据;</p>
<p>统计代码的PHP实现:</p>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">error_reporting</span>(<span style="color: rgba(255, 0, 255, 1)">E_ALL</span> & ~<span style="color: rgba(255, 0, 255, 1)">E_NOTICE</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">define</span>('ROOT_PATH', <span style="color: rgba(0, 128, 128, 1)">dirname</span>(<span style="color: rgba(255, 0, 255, 1)">__FILE__</span>) . '/'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$now</span> = <span style="color: rgba(0, 128, 128, 1)">date</span>('Y-m-d H:i:s'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$host</span> = '_null'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = @<span style="color: rgba(128, 0, 128, 1)">$_SERVER</span>['HTTP_REFERER'] ?: ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">))
{
</span><span style="color: rgba(128, 0, 128, 1)">$arr</span> = <span style="color: rgba(0, 128, 128, 1)">parse_url</span>(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$host</span> = <span style="color: rgba(128, 0, 128, 1)">$arr</span>['host'<span style="color: rgba(0, 0, 0, 1)">];
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$_GET</span>['host'<span style="color: rgba(0, 0, 0, 1)">]))
{
</span><span style="color: rgba(128, 0, 128, 1)">$host</span> = <span style="color: rgba(0, 128, 128, 1)">trim</span>(<span style="color: rgba(128, 0, 128, 1)">$_GET</span>['host'<span style="color: rgba(0, 0, 0, 1)">]);
</span><span style="color: rgba(128, 0, 128, 1)">$host</span> = <span style="color: rgba(0, 128, 128, 1)">str_replace</span>(['.', '/', '\\', ' '], '', <span style="color: rgba(128, 0, 128, 1)">$host</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(128, 0, 128, 1)">$file</span> = ROOT_PATH . '/stat/' . <span style="color: rgba(128, 0, 128, 1)">$host</span> . '/stat.log'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$str</span> = @<span style="color: rgba(0, 128, 128, 1)">file_get_contents</span>(<span style="color: rgba(128, 0, 128, 1)">$file</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$arr</span> =<span style="color: rgba(0, 0, 0, 1)"> [];
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$str</span><span style="color: rgba(0, 0, 0, 1)">))
{
</span><span style="color: rgba(0, 128, 128, 1)">mkdir</span>(ROOT_PATH . '/stat/' . <span style="color: rgba(128, 0, 128, 1)">$host</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$arr</span> = ;
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
{
</span><span style="color: rgba(128, 0, 128, 1)">$arr</span> = <span style="color: rgba(0, 128, 128, 1)">explode</span>(',', <span style="color: rgba(128, 0, 128, 1)">$str</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(128, 0, 128, 1)">$arr</span>++<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$arr</span> = <span style="color: rgba(0, 128, 128, 1)">substr</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>, 0, 10) < <span style="color: rgba(0, 128, 128, 1)">date</span>('Y-m-d') ? 0 : (<span style="color: rgba(128, 0, 128, 1)">$arr</span> + 1<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$arr</span> = <span style="color: rgba(128, 0, 128, 1)">$now</span><span style="color: rgba(0, 0, 0, 1)">;
@</span><span style="color: rgba(0, 128, 128, 1)">file_put_contents</span>(<span style="color: rgba(128, 0, 128, 1)">$file</span>, <span style="color: rgba(0, 128, 128, 1)">implode</span>(',', <span style="color: rgba(128, 0, 128, 1)">$arr</span><span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(128, 0, 128, 1)">$data</span> =<span style="color: rgba(0, 0, 0, 1)"> [
</span>'time' => <span style="color: rgba(128, 0, 128, 1)">$now</span>,
'ip' => <span style="color: rgba(128, 0, 128, 1)">$_SERVER</span>['REMOTE_ADDR'],
'user_agent' => @<span style="color: rgba(128, 0, 128, 1)">$_SERVER</span>['HTTP_USER_AGENT'] ?: '',
'url' => <span style="color: rgba(128, 0, 128, 1)">$url</span>,
'query' => @<span style="color: rgba(128, 0, 128, 1)">$_SERVER</span>['QUERY_STRING'] ?: '',<span style="color: rgba(0, 0, 0, 1)">
];
@</span><span style="color: rgba(0, 128, 128, 1)">file_put_contents</span>(ROOT_PATH . '/stat/' . <span style="color: rgba(128, 0, 128, 1)">$host</span> . '/' . <span style="color: rgba(0, 128, 128, 1)">date</span>('Ymd') . '.log', json_encode(<span style="color: rgba(128, 0, 128, 1)">$data</span>) . <span style="color: rgba(255, 0, 255, 1)">PHP_EOL</span>, LOCK_EX|<span style="color: rgba(0, 0, 0, 1)">FILE_APPEND);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$_GET</span>['null'<span style="color: rgba(0, 0, 0, 1)">]))
{
</span><span style="color: rgba(0, 0, 255, 1)">exit</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输出svg</span>
<span style="color: rgba(128, 0, 128, 1)">$out</span> = <span style="color: rgba(128, 0, 128, 1)">$arr</span> . '/' . <span style="color: rgba(128, 0, 128, 1)">$arr</span>;
</span><span style="color: rgba(128, 0, 128, 1)">$w</span> = <span style="color: rgba(0, 128, 128, 1)">strlen</span>(<span style="color: rgba(128, 0, 128, 1)">$out</span>) * 7 + 5<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">header</span>('Content-Type:image/svg+xml'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> <<<<span style="color: rgba(0, 0, 0, 1)">EOF
</span><svg version="1.1"<span style="color: rgba(0, 0, 0, 1)">
width</span>="<span style="color: rgba(128, 0, 128, 1)">$w</span>"<span style="color: rgba(0, 0, 0, 1)">
height</span>="15"<span style="color: rgba(0, 0, 0, 1)">
viewBox</span>="0 0 <span style="color: rgba(128, 0, 128, 1)">$w</span> 15"<span style="color: rgba(0, 0, 0, 1)">
preserveAspectRatio</span>="none"<span style="color: rgba(0, 0, 0, 1)">
xmlns</span>="http://www.w3.org/2000/svg"<span style="color: rgba(0, 0, 0, 1)">
xmlns</span>:xlink="http://www.w3.org/1999/xlink"
>
<text x="2" y="11" style="font-size:12px"><span style="color: rgba(128, 0, 128, 1)">$out</span></text>
</svg><span style="color: rgba(0, 0, 0, 1)">
EOF;
</span><span style="color: rgba(0, 0, 255, 1)">exit</span>;</pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<hr>
<b>
欢迎转载,转载请注明:转载自[
http://www.cnblogs.com/zjfree/
]</b><br><br>
来源:https://www.cnblogs.com/zjfree/p/12052964.html
頁:
[1]