-1

I have two buttons to change my language in my site.. i am getting the full url with

$myurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

And my two buttons:

echo '<a href="'.$myurl.'?lang=en"><img src="http://www.example.com/site/flags/en.png" name="English" title="English" alt="" width="40" height="40" border="0" ></a>';

echo '<a href="'.$myurl.'?lang=de"><img src="http://www.example.com/site/flags/de.png" name="German" title="German" alt="" width="40" height="40" border="0" ></a>';

So far it's working great... So if press the first button my url (in my url bar) will be

http://www.example.com/site/?lang=en

Now if i echo this "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; I am getting http://www.example.com/site/index.php and in my url bar i can see

http://www.example.com/site/?lang=en

Does anybody know why i can't "grab" the ?lang=en?

I also tried the code bellow but without luck....

if ($string == "http://www.example.com/site/index.php" || $string == "http://www.example.com/site/" || $string == "http://www.example.com/site/?lang=en" || $string == "http://www.example.com/site/index.php?lang=en"){
echo "ENGLISH";
}else{
echo "DE";
}
Irene T.
  • 1,393
  • 2
  • 20
  • 40
  • ever heard of `$_SERVER['PHP_SELF']`? Just do `echo " – nl-x Jun 01 '14 at 21:54
  • Please search first. Literally the exact same question was asked yesterday: http://stackoverflow.com/questions/23973574/get-full-website-url – Anonymous Jun 01 '14 at 21:55
  • 1
    Use "$_SERVER['QUERY_STRING']" to get parametters ! – Alpha Jun 01 '14 at 21:55
  • It returns "/site/index.php" – Irene T. Jun 01 '14 at 21:57
  • I checked this liks but doesn't work for me... My website is Wordpress.. Need i edit enything on it? All these returns http://www.example.com/site/ Even in my url bar is http://www.example.com/site/?lang=en – Irene T. Jun 01 '14 at 22:12

1 Answers1

1

So you want to read out lang from the url params?

if (!isset($_GET['lang']) || $_GET['lang'] == 'en')
    echo "ENGLISH";
else
    echo "DE";

If you just want to get the query string, to use in a link, use:

echo $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'];

Or to remove the ? when there is no query string:

echo $_SERVER['PHP_SELF'] . ($_SERVER['QUERY_STRING']!=''?'?':'') . $_SERVER['QUERY_STRING'];
nl-x
  • 11,762
  • 7
  • 33
  • 61