Say the user is loading the following page: www.example.com/myDirectory/myPage.php?answer=yes
And I want to use PHP to get myPage.php?answer=yes
from that. However, basename($_SERVER['PHP_SELF'])
only returns myPage.php
. How would I do that?
You can append the query string after you get it as an environment variable:
$myurl = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
If you need to consider the possibility of no query string, add a conditional test:
$myurl = strlen($_SERVER['QUERY_STRING']) ? basename($_SERVER['PHP_SELF'])."?".$_SERVER['QUERY_STRING'] : basename($_SERVER['PHP_SELF']);
it is easier?:
basename($_SERVER['REQUEST_URI'])
For URLs with trailing parameters the following will place the first part of the current URL (i.e. the part before ?) within the variable $current_url[0] and then print it out.
<?
$current_url = explode("?", $_SERVER['REQUEST_URI']);
echo $current_url[0] ;
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With