• 10
• 12
• 12
• 14
• 15

[web] Empty QueryString in PHP

This topic is 4314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Very simple question, well... it should be anyway. How do I check for an empty query string in PHP. I just wasted a few hours looking up information of the $_SESSION variable which I can't even seem to access. I'm starting to bald over this.  Ah, found the isset() function. Nevermind. [Edited by - lack o comments on July 2, 2006 12:02:48 PM] Share this post Link to post Share on other sites Advertisement well say you want to find if there is any value in the string$user_name all you do is:

if($user_name) { echo "You have a username."; } then if you want to do something if there is no value to it if(!$user_name)
{
echo "You do not have a username. Please go back and input one.":
}

Share on other sites
Quote:
 Original post by Raz Malonewell say you want to find if there is any value in the string $user_name all you do is:if($user_name) { echo "You have a username.";}then if you want to do something if there is no value to itif(!$user_name){ echo "You do not have a username. Please go back and input one.":} Note that the above code will work, but will generate a warning about an undeclared variable (if you have warnings enabled, which I recommend for testing/design purposes) You should use : if(isset($variable))

Share on other sites
Yeah, I come from a ASP background where this wouldn't even be an issue.

Apparently, there must be a setting in the PHP server to automatically fill out the $_SERVER array, namely$_SERVER['QUERY_STRING']. I haven't had time to figure out how to do this on my development computer, but the actual web host has it set.

The problem is, on my server if I attempt to check the contents of $_SERVER, it will tell me nothing is set and spit errors. I got around this by simply using "isset()" instead of checking for a value. (I need to know when the query string is empty when a page loads so I can go to the default home page). However, on the host's server$_SERVER is already filled out and "isset()" always returns true even if the element is empty.

To get around this I used:
if(isset($_SERVER['QUERY_STRING'])) { if($_SERVER['QUERY_STRING'] != "") {$PageID =$_SERVER['QUERY_STRING'];}	else	{$PageID = "Home";} }else {$PageID = "Home";}

Is that a good idea?

While I'm here, I have another issue. The host has the simple PHP tags enabled. which is conflicting with the XML at the top of my web pages
<?xml version="1.0" encoding="utf-8"?>

It keeps trying to read it as PHP script. Is there anyway from my standpoint I can fix this?

Share on other sites
PHP relies on the web server for the $_SERVER array. If you are having problems with that, I would solve it in a common.inc.php file like: if(!isset($_SERVER['QUERY_STRING'])) \$_SERVER['QUERY_STRING'] = "";

Then the rest of your code can just assume it is there. You should try to keep your page code as clean and trim as possible.

Regarding the <? problem, see http://us2.php.net/manual/en/ini.core.php#ini.short-open-tag.

Share on other sites
Quote:
 While I'm here, I have another issue. The host has the simple PHP tags enabled. which is conflicting with the XML at the top of my web pages

<?echo "<?xml version='1.0' encoding='utf-8'?>";?>