简体   繁体   中英

PHP url strings conflict “?” and “&”

I have a lang system in my website. When people click to switch to another language, the following will be added to the url: ?lang= . The problem is, my website is divided by pages which add ?p= to the url aswell. So basically, if they change the lang on one of those pages, it'll overwrite the ?p= and go back to the main page. So it'll be index.php?lang= .

What is the code or how should I code it so that php verifies if there's already a ? string in the url and switch ?lang= to &lang= ?

You can use http_build_query to avoid this problems:

$params = array(
    'p'    => 'foo',
    'lang' => 'bar'
);

echo http_build_query($params); // p=foo&lang=bar

echo '?' . http_build_query($params); // ?p=foo&lang=bar

check if $_SERVER['QUERY_STRING'] is empty.

if yes, add ?lang= otherwise add &lang=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM