URL extract ? [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

URL extract ? [solved]

Post 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 ? ?
Last edited by AGISB on Tue May 12, 2009 5:16 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: URL extract ?

Post 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
}
 
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Re: URL extract ?

Post 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 ?
Post Reply