开发者

function of $_SERVER['REQUEST_URI'] and $_SERVER['PHP_SELF']

I use this simple module included in my header to let the user change language of the site

 <div class="left first">
   <a href="' . $_SERVER['REQUEST_URI'] . $linkParam . 'sv">
    <img src="bilder/sv.png" alt="SV" />
   </a>
</div>


<div class="left"><img src="bilder/eng.png" alt="ENG" /></div>

The language available for change is a link that send the lang param via a http request and thus set the language session. Before I always sended the user to index.php but I would like to make it possible to change language throughout the site and sta开发者_StackOverflow社区y on the page.

Since some pages has http params which need to remain I use this solution to determine if the lang param should be ?lang=...or &lang=...

if($_SERVER['REQUEST_URI'] != $_SERVER['PHP_SELF']){

            $linkParam = '&lang=';

        }else{

            $linkParam = '?lang=';

        }

It is working fine with some test but will this consistently work to determine if the url already has ?param and set the lang-param?


Take a look at the http_build_query function, perhaps this will help you. You could do the following:

$aGet = $_GET;
$aGet['lang'] = $sLanguage;
$sQueryString = http_build_query($aGet);


It would be better to use:

if(strpos($_SERVER['REQUEST_URI'], '?') !== false)
    $linkParam = '&lang=';
else
    $linkParam = '?lang=';

Or even better actually:

if($_SERVER['QUERY_STRING'])
    $linkParam = '&lang=';
else
    $linkParam = '?lang=';

PHP_SELF could be changed by something like a .htaccess file while REQUEST_URI stays the same. So if you even started to use mod_rewrite it could stop working your way. I think QUERY_STRING is more cross-server than REQUEST_URI as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜