Page 1 of 1

URL extract ? [solved]

Posted: Mon May 11, 2009 7:33 am
by AGISB
I got a very weird problem I haven't before thought about.

When I call my page with ...index.php or index.php? it displays the same site. I only want to show index.php and when index.php? shows up I want to make a header Location request to index.php. (There is nothing behind the ?)

My problem now is, that I cannot determine if there simply is just the ?

I tried isset and array_key_exists on $_SERVER['QUERY_STRING']) but it seems the array is there nomatter what and therefore both index.php and index.php? always have the same result.

Script_Name displays without ?

So is there anyway to eliminate that ? ?

Re: URL extract ?

Posted: Mon May 11, 2009 10:58 am
by John Cartwright
You can use trim with the optional second argument.

Code: Select all

$querystring = isset($_SERVER['QUERY_STRING']) ? trim($_SERVER['QUERY_STRING'], '?') : '';
if (empty($querystring)) {
   //do something
} else {
   //do something else
}
 

Re: URL extract ?

Posted: Mon May 11, 2009 2:13 pm
by AGISB
John Cartwright wrote:You can use trim with the optional second argument.

Code: Select all

$querystring = isset($_SERVER['QUERY_STRING']) ? trim($_SERVER['QUERY_STRING'], '?') : '';
if (empty($querystring)) {
   //do something
} else {
   //do something else
}
 
The Problem is, that the ? is neither part of the $_SERVER['QUERY_STRING'] or the $_SERVER['SCRIPT_NAME'] so I can't trim any of those ;-(

I found the way however, just for future reference

$_SERVER['REQUEST_URI'] shows the complete path and so I could just determine if the last char is ?