This time I will show you how to operate PHP to record visitor browsing information. What are the precautions for PHP to record visitor browsing information? Here is a practical case, let’s take a look.
Can record the visitor’s geographical location, operating system, browser, IP, time and accessed files.
1. First create a comm_function.php file:
<?php //echo $_SERVER['HTTP_USER_AGENT']; //echo "<br />".$_SERVER ['REMOTE_ADDR']; /** * 获取客户端类型,手机还是电脑,以及相应的操作系统类型。 * * @param string $subject */ function get_os($agent) { $os = false; if (preg_match ( '/win/i', $agent ) && strpos ( $agent, '95' )) { $os = 'Windows 95'; } else if (preg_match ( '/win 9x/i', $agent ) && strpos ( $agent, '4.90' )) { $os = 'Windows ME'; } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/98/i', $agent )) { $os = 'Windows 98'; } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.0/i', $agent )) { $os = 'Windows Vista'; } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.1/i', $agent )) { $os = 'Windows 7'; } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.2/i', $agent )) { $os = 'Windows 8'; } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 10.0/i', $agent )) { $os = 'Windows 10'; // 添加win10判断 } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 5.1/i', $agent )) { $os = 'Windows XP'; } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 5/i', $agent )) { $os = 'Windows 2000'; } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt/i', $agent )) { $os = 'Windows NT'; } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/32/i', $agent )) { $os = 'Windows 32'; } else if (preg_match ( '/linux/i', $agent )) { if(preg_match("/Mobile/", $agent)){ if(preg_match("/QQ/i", $agent)){ $os = "Android QQ Browser"; }else{ $os = "Android Browser"; } }else{ $os = 'PC-Linux'; } } else if (preg_match ( '/Mac/i', $agent )) { if(preg_match("/Mobile/", $agent)){ if(preg_match("/QQ/i", $agent)){ $os = "IPhone QQ Browser"; }else{ $os = "IPhone Browser"; } }else{ $os = 'Mac OS X'; } } else if (preg_match ( '/unix/i', $agent )) { $os = 'Unix'; } else if (preg_match ( '/sun/i', $agent ) && preg_match ( '/os/i', $agent )) { $os = 'SunOS'; } else if (preg_match ( '/ibm/i', $agent ) && preg_match ( '/os/i', $agent )) { $os = 'IBM OS/2'; } else if (preg_match ( '/Mac/i', $agent ) && preg_match ( '/PC/i', $agent )) { $os = 'Macintosh'; } else if (preg_match ( '/PowerPC/i', $agent )) { $os = 'PowerPC'; } else if (preg_match ( '/AIX/i', $agent )) { $os = 'AIX'; } else if (preg_match ( '/HPUX/i', $agent )) { $os = 'HPUX'; } else if (preg_match ( '/NetBSD/i', $agent )) { $os = 'NetBSD'; } else if (preg_match ( '/BSD/i', $agent )) { $os = 'BSD'; } else if (preg_match ( '/OSF1/i', $agent )) { $os = 'OSF1'; } else if (preg_match ( '/IRIX/i', $agent )) { $os = 'IRIX'; } else if (preg_match ( '/FreeBSD/i', $agent )) { $os = 'FreeBSD'; } else if (preg_match ( '/teleport/i', $agent )) { $os = 'teleport'; } else if (preg_match ( '/flashget/i', $agent )) { $os = 'flashget'; } else if (preg_match ( '/webzip/i', $agent )) { $os = 'webzip'; } else if (preg_match ( '/offline/i', $agent )) { $os = 'offline'; } else { $os = '未知操作系统'; } return $os; } /** * 获取 客户端的浏览器类型 * @return string */ function get_broswer($sys){ if (stripos($sys, "Firefox/") > 0) { preg_match("/Firefox\/([^;)]+)+/i", $sys, $b); $exp[0] = "Firefox"; $exp[1] = $b[1]; //获取火狐浏览器的版本号 } elseif (stripos($sys, "Maxthon") > 0) { preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou); $exp[0] = "傲游"; $exp[1] = $aoyou[1]; } elseif (stripos($sys, "MSIE") > 0) { preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie); $exp[0] = "IE"; $exp[1] = $ie[1]; //获取IE的版本号 } elseif (stripos($sys, "OPR") > 0) { preg_match("/OPR\/([\d\.]+)/", $sys, $opera); $exp[0] = "Opera"; $exp[1] = $opera[1]; } elseif(stripos($sys, "Edge") > 0) { //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配 preg_match("/Edge\/([\d\.]+)/", $sys, $Edge); $exp[0] = "Edge"; $exp[1] = $Edge[1]; } elseif (stripos($sys, "Chrome") > 0) { preg_match("/Chrome\/([\d\.]+)/", $sys, $google); $exp[0] = "Chrome"; $exp[1] = $google[1]; //获取google chrome的版本号 } elseif(stripos($sys,'rv:')>0 && stripos($sys,'Gecko')>0){ preg_match("/rv:([\d\.]+)/", $sys, $IE); $exp[0] = "IE"; $exp[1] = $IE[1]; }else { $exp[0] = "未知浏览器"; $exp[1] = ""; } return $exp[0].'('.$exp[1].')'; } /** * 根据 客户端IP 获取到其具体的位置信息 * @param unknown $ip * @return string */ function get_address_by_ip($ip) { $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $info = curl_exec($curl); curl_close($curl); return $info; } function clientlog() { $useragent = $_SERVER ['HTTP_USER_AGENT']; $clientip = $_SERVER ['REMOTE_ADDR']; $client_info = get_os ( $useragent ) . "---" . get_broswer ( $useragent ); $rawdata_position = get_address_by_ip ( $clientip ); $rawdata_position = json_decode($rawdata_position, true); $country = $rawdata_position['data']['country']; $province = $rawdata_position['data']['region']; $city = $rawdata_position['data']['city']; $nettype = $rawdata_position['data']['isp']; $time = date ( 'y-m-d h:m:s' ); $data = "来自{$country} {$province} {$city }{$nettype} 的客户端: {$client_info},IP为:{$clientip},在{$time}时刻访问了{$_SERVER['PHP_SELF']}文件!\r\n"; $filename = "./log.log"; if (! file_exists ( $filename )) { fopen ( $filename, "w+" ); } file_put_contents ( $filename, $data, FILE_APPEND ); }
2. Introduce this comm_function into other files .php
require_once "comm_function.php";
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
php Detailed steps to delete value elements in a one-dimensional array
php steps to install extensions through pecl Detailed explanation
The above is the detailed content of How PHP can record visitor browsing information. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
