0

这是我的javascript代码

<script type="text/javascript">
var country,url;
country = geoip_country_code()
if(country=="US"){
    url="http://www.site.org/index.php?user=php variable";
}
setTimeout("location.href = url;",5000);
</script>

我需要将$_GET['user']php 变量用于在当前 url 中获取用户名

试过这个但不起作用

if(country=="US"){
    url="http://www.site.org/index.php?user=<?php echo $_GET['user']; ?>";
}
4

3 回答 3

4

你可以用普通的 javascript 做到这一点:

var query = (function() {
    var result = {}, keyValuePairs = location.search.slice(1).split('&');

    keyValuePairs.forEach(function(keyValuePair) {
        keyValuePair = keyValuePair.split('=');
        result[keyValuePair[0]] = keyValuePair[1] || '';
    });

    return result;
})();

if(country=="US"){
    url="http://www.site.org/index.php" + (query.user !== undefined) ? "?user=" + query.user : "";
}

有关不确定的细节,请参阅https://stackoverflow.com/a/647272/838733

于 2013-10-20T19:34:53.393 回答
0

这是使用纯 PHP 的版本:

$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=$_SERVER['HTTP_CLIENT_IP']";
$country = geoplugin_countryCode;
if($country=="US"){ 
    $url="http://www.site.org/index.php?user=$_GET['user']"; 
}
sleep(5);
header("Location: $url");
于 2013-10-20T19:57:39.423 回答
-1

假设$_GET['user']设置:

var user = "<?php echo $_GET['user']; ?>";
url="http://www.site.org/index.php?user="+user;
于 2013-10-20T19:45:01.967 回答