【问题标题】:How to create redirect page to new page如何创建重定向页面到新页面
【发布时间】:2014-03-30 19:35:46
【问题描述】:

我不是专家,但我正在尝试监控我的流量我试图使用检测用户地理并将他们重定向到新页面的代码创建页面

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

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

但我找不到这个 GeoIP.php 的位置

有人可以给我提示我需要什么来使它工作

谢谢 波阿斯

【问题讨论】:

标签: php redirect geoip


【解决方案1】:

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

简单地使用

$country = geoip_continent_code_by_name ( $_SERVER['REMOTE_ADDR'] );

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

<?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

?>

您会注意到我删除了 try, catch 块。这是用户偏好,但大多数人不喜欢它(使用 switch 案例,这就是您提供默认案例的原因)。

这对你应该 100% 有效。

【讨论】:

  • 如果您从我的帖子中了解到我不是专家,我需要有助于完成这项工作的代码,我完全是菜鸟
猜你喜欢
  • 2010-11-08
  • 2015-01-15
  • 2021-04-05
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
相关资源
最近更新 更多