1

I am trying to build a language switch for a site of mine, but, as the hosting MUST BE a Windows IIS with PHP, I am not able to get the full URL of the page viewed by the visitor.

Say I am on domain.com/page.php?id=23, what I get using the old fashioned

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

is an empty string.

So I have tried a solution found here on stackoverflow to get the full URL

function getFullUrl() {     
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";          
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER['HTTP_X_REWRITE_URL'];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER['HTTP_X_REWRITE_URL'];//$_SERVER["REQUEST_URI"] blank
    }
    return $pageURL;
}

but still nothing. I get something like: domain.com/page.php? in some cases and doman.com/? in others.

Is there a definitive way to get the full url in that scenario?

Thank you very much.

EDIT

It seems that the thing cannot be done on that server. I solved using a client side work around written in js+jQuery (I used purl plugin)....horrible :) Here it is:

    $('.lang').each(function(){
        $(this).click(function(e){
            e.preventDefault();
            var lingua = $(this).find('a').data('lang');
            var url = window.location.href;
            var urlParts = purl(url);

            if (url.indexOf("?") >= 0)
            {               
                var queryString = urlParts.attr('query').split('&');
                var langIndex = queryString.indexOf('lang');

                if(langIndex > -1)
                {
                    queryString.splice(langIndex,1);
                    var newUrl = urlParts.attr('protocol')+urlParts.attr('host')+urlParts.attr('path')
                    window.location = newUrl + '&lang='+lingua;
                }                   
                else window.location = url + '&lang='+lingua;
            }
            else
                window.location = url + '?lang='+lingua;
        });     
    });

It seems to work, even if I don't get why if "lang" is already in the query string, the script doesn't recognize it and continues adding &lang every time I click on the links. Sure, it works anyway, but it is horrible to watch a URL like &lang=en&lang=fr&lang=pl&lang=de

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aptivus
  • 433
  • 1
  • 8
  • 24
  • Is anything returned by getFullURL() or just a truncated URL? – The One and Only ChemistryBlob Sep 12 '16 at 15:55
  • It returns just domain.com, nothing more. That's frustrating – Aptivus Sep 12 '16 at 16:00
  • Did you see this answer http://stackoverflow.com/questions/189113/how-can-i-get-the-current-pages-full-url-on-a-windows-iis-server ? The top answer is to use `$_SERVER['PATH_INFO']` on IIS which you don't appear to be using. Try that in your getFullUrl function. – R. Chappell Sep 12 '16 at 16:09
  • @R.Chappell Still an empty string. – Aptivus Sep 12 '16 at 16:24
  • Perhaps you should `var_dump($_SERVER);` and see what you have to play with. It should be populated with a bunch of stuff and if it isn't then there might be an issue with your server / host. – R. Chappell Sep 12 '16 at 16:27

1 Answers1

0

This line returns empty

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

because it should be this:

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

You need quotes for the keys....see here in the manual.

  • Well, on Linux it can be written the way I did. By the way, your suggestion solved part of the problem. Now it seems that if I am on a URL without query string, it works perfectly. On the other hand, if a query string is already there, then it doesn't work. It just shows the old query string – Aptivus Sep 12 '16 at 16:15