Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current filename with URL parameters

Tags:

php

filenames

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?

like image 840
Mooseman Avatar asked May 13 '13 18:05

Mooseman


3 Answers

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']);
like image 90
Blazemonger Avatar answered Nov 15 '22 06:11

Blazemonger


it is easier?:

basename($_SERVER['REQUEST_URI'])
like image 27
Gökalp Turan Avatar answered Nov 15 '22 04:11

Gökalp Turan


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] ;
?>
like image 1
Shiv Singh Avatar answered Nov 15 '22 05:11

Shiv Singh