Reputation: 627
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?
Upvotes: 1
Views: 348
Reputation: 3153
I found this code very helpful
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') ===
FALSE ? 'http' : 'https'; // Get protocol HTTP/HTTPS
$host = $_SERVER['HTTP_HOST']; // Get www.domain.com
$script = $_SERVER['SCRIPT_NAME']; // Get folder/file.php
$params = $_SERVER['QUERY_STRING'];// Get Parameters occupation=odesk&name=ashik
$currentUrl = $protocol . '://' . $host . $script . '?' . $params; // Adding all
echo $currentUrl;
Upvotes: 0
Reputation: 10646
$_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:
$uri = explode('?', $_SERVER['REQUEST_URI']);
$uri = $uri[0];
And then use the $uri
variable in place of $_SERVER['REQUEST_URI']
like this:
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$uri : "http://".$_SERVER['SERVER_NAME'].$uri;
Upvotes: 4