Get current filename with URL parameters

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?

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']);

it is easier?:

basename($_SERVER['REQUEST_URI'])

$_SERVER['QUERY_STRING']

will give you the paramaters


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] ;
?>

Need Your Help

Postgres - Combining SELECT and UPDATE

php postgresql pdo

I have a table with messages in a Postgres DB.

SQL table referencing multiple foreign keys, while defining order and guaranteeing referential integrity

c# sql database-design foreign-keys

My customer has specified, that he needs different actions to be executed, when certain conditions in an internal logic are met. Each action type (so far Command and WriteVariable) has an individua...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.