1 获取当前访客用户画像信息

php可以获取当前访客的ip地址,所访问的页面url,入口页url,访客的操作系统、访客使用何种浏览器访问该页面,这对我们掌握用户特征,调整网站内容结构具有重要的作用。

1.1 获取用户ip地址

通过以下代码获取用户ip地址

// 获取访问者IP地址
$ip = '0.0.0.0';
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP") , "unknown")) {
    $ip = getenv("HTTP_CLIENT_IP");
} else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR") , "unknown")) {
    $ip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR") , "unknown")) {
    $ip = getenv("REMOTE_ADDR");
} else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
    $ip = $_SERVER['REMOTE_ADDR'];
} else {
    $ip = "unknown";
}

1.2 获取操作系统类型

通过以下代码获取用户操作系统类型

$userAgent = empty($_SERVER['HTTP_USER_AGENT']) ? 'null' : $_SERVER['HTTP_USER_AGENT'];
// 判断访问系统
$system = "unknown";
$ios_regex="/(iPhone OS|iPad)/i";
if(false !== stripos($userAgent, 'Android')){
    $system = "Android";
}else if(false !== stripos($userAgent, 'Windows Phone')){
    $system = "Windows Phone";
}else if(false !== stripos($userAgent, 'Windows NT')){
    $system = "Windows";
}else if(preg_match($ios_regex, $userAgent)){
    $system = "IOS";
}else if(false !== stripos($userAgent, 'Mac')){
    $system = "MacOS";
}else if(false !== stripos($userAgent, 'Linux')){
    $system = "Linux";
}else if(false !== stripos($userAgent, 'Unix')){
    $system = "Unix";
}

1.3 获取用户使用的浏览器类型

通过以下代码获取用户浏览器类型

$userAgent = empty($_SERVER['HTTP_USER_AGENT']) ? 'null' : $_SERVER['HTTP_USER_AGENT'];
$client = "unknown";
$ie_regex="/(MSIE|Trident)/i";
if(stripos($userAgent, 'MicroMessenger')){
    $client = "WeiXin";
}else if(stripos($userAgent, 'AlipayClient')){
    $client = "Alipay";
}else if(stripos($userAgent, 'QQTheme')){
    $client = "QQ";
}else if(stripos($userAgent, 'HeyTapBrowser')){
    $client = "HeyTap";
}else if(stripos($userAgent, 'VivoBrowser')){
    $client = "Vivo";
}else if(stripos($userAgent, 'MQQBrowser')){
    $client = "QQ浏览器移动端";
}else if(stripos($userAgent, 'MQQBrowser')){
    $client = "QQ浏览器";
}else if(stripos($userAgent, 'baiduboxapp')){
    $client = "百度app";
}else if(stripos($userAgent, 'weibo')){
    $client = "WeiBo";
}else if(stripos($userAgent, 'iqiyi')){
    $client = "爱奇艺";
}else if(stripos($userAgent, 'aweme')){
    $client = "抖音";
}else if(stripos($userAgent, 'kdtUnion_iting')){
    $client = "喜马拉雅";
}else if(stripos($userAgent, 'UCBrowser')){
    $client = "UC";
}else if(stripos($userAgent, 'DingTalk')){
    $client = "钉钉";
}else if(stripos($userAgent, 'SamsungBrowser')){
    $client = "三星浏览器";
}else if(stripos($userAgent, 'HUAWEIEVA')){
    $client = "华为浏览器";
}else if(stripos($userAgent, 'NokiaBrowser')){
    $client = "诺基亚浏览器";
}else if(stripos($userAgent, 'Opera')){
    $client = "Opera";
}else if(stripos($userAgent, 'Firefox')){
    $client = "Firefox";
}else if(stripos($userAgent, 'Chrome')){
    $client = "Chrome";
}else if(stripos($userAgent, 'Safari')){
    $client = "Safari";
}else if(preg_match($ie_regex, $userAgent)){
    $client = "IE";
}

1.4 获取用户所使用的语言

通过以下代码获取用户所使用的语言

$lang = "unknown";
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $lang = substr($lang, 0, 5);
    if (preg_match("/zh-cn/i", $lang)) {
        $lang = "简体中文";
    } elseif (preg_match("/zh/i", $lang)) {
        $lang = "繁体中文";
    } else {
        $lang = "English";
    }
}

1.5 获取用户访问网站的入口页url

通过以下代码获取用户用户访问网站的入口页url

$referer_url = $_SERVER['HTTP_REFERER'];

1.6 获取用户当前访问的页面url

通过以下代码获取用户当前访问的页面url

$current_page_url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

参考