基于地理位置的PHP IF语句? [英] PHP IF Statment Based On Geo Location?

查看:190
本文介绍了基于地理位置的PHP IF语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种基于人员位置的PHP If语句的方法?

Is there a way to do a PHP If statement based on the persons location?

我的问题归结于Amazon Affiliate链接.在Amazon.co.uk上购买DVD的链接与从Amazon.com上购买的DVD的链接不同,我想要一种只显示正确的DVD的方法.

My problem comes down to Amazon Affiliate links. The link to buy a DVD on Amazon.co.uk is different to the one to buy from Amazon.com and I want a way to only show the correct one.

同时,如果他们不在两个国家/地区,那么我不希望链接显示在第一位.

At the same time, If they aren't based in either country, then I don't want the link to show in the first place.

示例:

If Location = UK; print "amazon-UK-link"
If Location = US; print "amazon-US-link"
If location = None of the above; print nothing

推荐答案

您将不得不使用访问者的IP地址来查找其实际位置.除了使用PHP的GeoIP扩展(如stewe所指出的那样)外,还有两种方法可以做到这一点:

You're going to have to use the visitor's IP address to lookup their physical location. Apart from using the GeoIP extension for PHP (as stewe pointed out), there are two ways of doing this:

简便方法

使用外部服务,例如 http://www.hostip.info

使用您自己的MySQL数据

1.)检索访问者的IP地址:

1.) Retrieve the visitors' IP address:

if (getenv('HTTP_X_FORWARDED_FOR')) 
{
    $ip_address = getenv('HTTP_X_FORWARDED_FOR');
} 
else 
{
    $ip_address = getenv('REMOTE_ADDR');
}

2.)将访问者的IP地址转换为IP地址:

2.) Convert the visitor's IP address to an IP Number:

$ips = explode(".",$ip_address);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);

3.)从数据库中找到IP地址,您可以

3.) Locate the IP Number from your database which you can download here. For example: the IP Address 202.186.13.4 converts to IP Number 3401190660. It is between the beginning and the ending of the following IP numbers:

Beginning_IP | End_IP      | Country  | ISO
-------------+-------------+----------+----
3401056256   | 3401400319  | MALAYSIA | MY

这篇关于基于地理位置的PHP IF语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆