I am trying to get the full url name for example
https://example.com/index.php?page=home
I use this code
$curPageName = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/") 1);
and this code
<a href="https://example.com/<?php echo $curPageName; ?>" title="Check this site in English">
Unfortunately the result comes out as
the rest or url "?page=home" is missing. How can I get this included? its not just only one page like ?page=home is has more pages. Do I miss something in substr($_SERVER["SCRIPTNAME"] ???
CodePudding user response:
Use $_SERVER['QUERY_STRING']
to get the query string and make a function that inserts a ?
if there are indeed values in the query.
function get_query_string() {
$query = $_SERVER['QUERY_STRING'];
if(strlen($query) > 0) {
$query = "?" . $query;
}
return $query;
}
$curPageName = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/") 1) . get_query_string();
CodePudding user response:
Use $_SERVER['REQUEST_URI']
https://google.com/foo/bar/?test=3
returns
/foo/bar/?test=3
In a link: <a href="https://example.com<?php echo $_SERVER['REQUEST_URI']; ?>" title="Link">
A few notes about using this:
- Be aware that the client can set REQUEST_URI to any value
- REQUEST_URI might include the scheme and domain when calling the page through stream_context_create() with a HTTP header of 'request_fulluri' set to 1.
Additional Questions: