I’m trying to get current page url. Here is the code:
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
It works fine. But I’m dealing with problem:
<a title="LT" href="<?php echo $url; ?>?lang=lt">LT</a>
When I press “LT” link once again, it gives me result:
http://127.0.0.1/index.php?lang=lt?lang=lt
How to avoid this?
$_SERVER['REQUEST_URI']returns everything after the domain including the querystring (Well… not everything, since it can’t return fragments…)If you want to use
$_SERVER['REQUEST_URI']then you could explode it like so:And then use the
$urivariable in place of$_SERVER['REQUEST_URI']like this: