首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建重定向页到新页

如何创建重定向页到新页
EN

Stack Overflow用户
提问于 2014-03-30 19:35:46
回答 1查看 245关注 0票数 0

我不是专家,但我试图监控我的流量,我试图用代码创建页面,以检测用户Geo并将他们重定向到新页面。

但它看起来像是缺少了什么我不知道是什么

我试着使用这个Geo redirect user just once using php

但是我找不到我从哪里得到这个GeoIP.php

有人能告诉我我需要什么才能成功吗?

谢谢波阿兹

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-30 19:58:47

你应该看看这个:http://www.php.net/manual/en/function.geoip-continent-code-by-name.php

简单地使用

代码语言:javascript
复制
$country = geoip_continent_code_by_name ( $_SERVER['REMOTE_ADDR'] );

$country将返回2个字母的国家代码。以下是您应该能够使用的内容:

代码语言:javascript
复制
<?php

   //Get the country code of the user, using REMOTE_ADDR is the most reliable way to do this
   $country = geoip_continent_code_by_name ( $_SERVER['REMOTE_ADDR'] );

  //Create a switch case to deal with the country codes
  switch((string)$country) {
    case 'AU':
      $url = "http://www.site.au";
      break;
    case 'CA':
      $url = "http://www.site.ca";
      break;

    //default case means, if you didn't provide a case (country code in your example), do this (otherwise known as a "catch all")
    default:
      $url = "http://defaultsite.com";

      } //End switchcase

  //This if statement says as long as $url is not empty (! means not), update header location (this is the php way of forwarding)
  if ( !empty($url) ){
      header('Location: '.$url);
  } //End if statement

?>

你会注意到我删除了尝试,捕捉块。这是用户偏好,但是,大多数人不喜欢它(使用开关大小写,这就是为什么您提供默认情况)。

这对你来说应该是100%的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22748819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档