dtgj8529 2017-07-19 10:35
浏览 89

在国家/地区代码中设置国家/地区代码并使用国家/地区代

I want to set country code in url by user IP address and rewrite and redirect index2.php with country code.

The index.php file:

<?php
function getLocationInfoByIp(){
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = @$_SERVER['REMOTE_ADDR'];
    $result  = array('country'=>'', 'city'=>'');
    if(filter_var($client, FILTER_VALIDATE_IP)){
        $ip = $client;
    }elseif(filter_var($forward, FILTER_VALIDATE_IP)){
        $ip = $forward;
    }else{
        $ip = $remote;
    }
    $ip_data = @json_decode
    (file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));    
    if($ip_data && $ip_data->geoplugin_countryName != null){
        $result['country'] = $ip_data->geoplugin_countryCode;
    }
    return $result;
}    
$result = getLocationInfoByIp($_SERVER['REMOTE_ADDR']);    
$cont = $result['country'];    
?>

The index2.php file:

<h1>HELLO WORLD !</h1>

how can I rewrite the url and redirect in index2.php?

When redirect my url should looks like orchidkart.com/IN/index2.php

  • 写回答

1条回答 默认 最新

  • douquan2023 2017-07-19 10:46
    关注

    Try

    header('Location: index2.php?country='.$cont);
    

    OR Full path

    header('Location: http://localhost/directory/index2.php?country='.$cont);
    

    OR parse dynamic variables in url

    header('Location: http://localhost/directory/$cont/index2.php');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染