search
HomeBackend DevelopmentPHP Tutorial #舍得Share#php开发札记-通过IP区分不同国家的用户

#舍得Share#php开发笔记-通过IP区分不同国家的用户

来自:http://lwxshow.com/distinguish-between-users-in-different-countries-by-ip

区分访客国家有什么用?

区分网站功能
这个博客有翻译文章的功能, 这是为了方便海外访客阅读文章, 但对中国人显得十分多余. 所以我通过 IP 判断国家, 对中国大陆地区屏蔽翻译功能.

区分展示广告
我在网站侧边栏放有广告, 很多中文广告对海外流量来说毫无意义, 所以我进行区分展示. 中国大陆地区在侧边栏最下方看到的是拿福能的广告, 而其他地区看到的是 Google 的广告.?hostucan?是我的一个广告主, 有英文网站, 也有中文网站, 所以我可以向他提供区分展示服务, 免得浪费流量.

屏蔽布点服务
海外有很多好的服务平台, 在网站上布点即可采集数据和分享文章. 但很不幸, 因为某些原因, 他们在国内展示效果并不好, 不但没有起到应有效果, 还让页面加载时间变长. 可以对大陆访客屏蔽这些布点.

在 PHP 通过 IP 区分国家

何用 PHP 通过 IP 区分国家和地区呢??Maxmind.com ?提供一套 GeoIP 的解决方案, 只需要简单几步即可在 PHP 中通过 IP 判断访客的国家.

1. 下载数据库和 PHP 库文件

  • 下载?GeoID.dat.gz , 解压为 GeoIP.dat 文件.
  • 下载?geoip.inc?php1.11.tar.gz
  • 国内打包高速下载地址:http://kuai.xunlei.com/d/PTQLLLMPISUX

2. 通过 PHP 代码获取国家信息
以下是一段示范代码, 演示如何获取国家代号和国家名称.

<span style="color: #008000;"><?php ? // 引入 PHP 库文件 include("geoip.inc"); ? // 打开本地数据库, 数据保存在 GeoIP 文件中. $geoData = geoip_open('GeoIP.dat', GEOIP_STANDARD); ? // 获取国家 IP $countryCode = geoip_country_code_by_addr($geoData, $_SERVER['REMOTE_ADDR']); ? // 获取国家名称 $countryName = geoip_country_name_by_addr($geoData, $_SERVER['REMOTE_ADDR']); ? // 关闭本地数据库 geoip_close($geoData); ? ?></span>
<span style="color: #008000;">
</span>

3. PHP 代码示例 :http://lwxshow.com/?p=2542

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

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

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

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

PHP8.0中的地理位置处理库:GeoIPPHP8.0中的地理位置处理库:GeoIPMay 14, 2023 am 08:30 AM

随着数字时代的到来,地理位置处理变得越来越重要。对于很多Web开发者和数据分析师而言,定位用户的地理位置已经不再是一个困难的事情。PHP8.0中的GeoIP即是一款地理位置处理库,为开发者提供了精准定位和处理用户位置的功能。本文将简要介绍GeoIP的使用方法和应用。什么是GeoIPGeoIP是一款由MaxMind开发的PHP扩展,用

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

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

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

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

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

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

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

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

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software