$ _GET ['user'] php变量在javascript代码中

时间:2013-10-20 19:29:16

标签: javascript php variables

这是我的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变量用于获取当前网址中的用户名

尝试了这个,但没有工作

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

3 个答案:

答案 0 :(得分: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 : "";
}

有关iffy的详细信息,请参阅https://stackoverflow.com/a/647272/838733

答案 1 :(得分: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");

答案 2 :(得分:-1)

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

var user = "<?php echo $_GET['user']; ?>";
url="http://www.site.org/index.php?user="+user;