0

Here is my javascript code

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

i need put $_GET['user'] php variable for get username in the current url

tried with this but not work

if(country=="US"){
    url="http://www.site.org/index.php?user=<?php echo $_GET['user']; ?>";
}
nietonfir
  • 4,797
  • 6
  • 31
  • 43
user2893064
  • 11
  • 1
  • 1
  • 1
    You can't mix PHP (server side) with javascript (client side). You can output the value of $_GET['user'] to the page with PHP or you can get the querystring variable with javascript. – Popnoodles Oct 20 '13 at 19:33
  • 2
    This should work if the code executed as PHP code. Could you give more context? Also, this is probably a major security issue. – Rengers Oct 20 '13 at 19:34
  • 1
    your code seems to be correct can you give us the exact output it is giving –  Oct 20 '13 at 19:35
  • I have done this several times, so the problem is other – Tomas Ramirez Sarduy Oct 20 '13 at 19:37
  • 1
    @popnoodles — So what? The PHP engine doesn't care if some of the text it is outputting happens to be ` – Quentin Oct 20 '13 at 19:38
  • The JS isn't being outputted by PHP. If it was it would work and they wouldn't be asking the question. – Popnoodles Oct 20 '13 at 19:39
  • @popnoodles — Anything outside of `` will be outputted by the PHP engine when passed through it. – Quentin Oct 20 '13 at 19:39
  • @Quentin where does OP say they are including ` – Popnoodles Oct 20 '13 at 19:40
  • 1
    @popnoodles — Since the code includes a script element, it is presumably inside an HTML document. They don't say if that HTML document has a `.php` file extension or not. If it doesn't (and it doesn't use an alternative method for passing it through the PHP engine) then the solution is "Give the file a .php extension" and not "you can't mix JavaScript and PHP" – Quentin Oct 20 '13 at 19:41
  • No they don't. The question is too ambiguous. However, if that was all in a PHP file, tell me why it's not working. It seems obvious to me that that's javascript in a javascript file or in HTML that isn't outputted by PHP. – Popnoodles Oct 20 '13 at 19:42
  • @user2893064 — Define "doesn't work". What is the outputted JavaScript (which you can see with View > Source)? What (if any) messages are displayed in your browser's JS console? What value are you giving to `user` in the query string? – Quentin Oct 20 '13 at 19:43
  • @user2893064 can you tell us where that code is? How is it included in the page? – Popnoodles Oct 20 '13 at 19:44
  • @popnoodles — There are several possibilities. The question doesn't have enough information to make it worth speculating at present. – Quentin Oct 20 '13 at 19:45
  • code works in my localhost. Code executes first on server side so the value echos first. No problem there. if the guy wanted to get value by javascript in a php function that would be a problem. – cowboysaif Oct 20 '13 at 20:20
  • thaks guys, yes the file is with .php extension. – user2893064 Oct 20 '13 at 21:22
  • @user2893064 — Define "doesn't work". What is the outputted JavaScript (which you can see with View > Source)? What (if any) messages are displayed in your browser's JS console? What value are you giving to user in the query string? – Quentin Oct 21 '13 at 10:23

3 Answers3

4

You can do this in plain 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 : "";
}

See https://stackoverflow.com/a/647272/838733 for the iffy specifics.

Community
  • 1
  • 1
nietonfir
  • 4,797
  • 6
  • 31
  • 43
0

This is the version using pure 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");
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
-1

Assuming $_GET['user'] is set:

var user = "<?php echo $_GET['user']; ?>";
url="http://www.site.org/index.php?user="+user;
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23