我想根据用户的IP地址在网址中设置国家/地区代码,并使用国家/地区代码重写和重定向index2.php
。
index.php
文件:
<?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'];
?>
index2.php
文件:
<h1>HELLO WORLD !</h1>
如何在index2.php
中重写网址和重定向?
重定向我的网址时应该看起来像orchidkart.com/IN/index2.php
答案 0 :(得分:1)
尝试
header('Location: index2.php?country='.$cont);
OR完整路径
header('Location: http://localhost/directory/index2.php?country='.$cont);
或解析url中的动态变量
header('Location: http://localhost/directory/$cont/index2.php');