Jump to content

Dealing with non-present GET variable?


FlyingIsFun1217

Recommended Posts

Hey,

 

I'm trying to get a page that goes through different situations via the GET variable. Because of this, I need to be able to deal with not having anything in the GET array (if the user just goes to x.php). The way I have it now is something like such:

 

if (!array_key_exists('page', $_GET) && !empty($_GET))
{
	//Put in template code for error page
	print 'Wrong GET name!';
}
else
{
//Page 1 - Welcome, process explanation
if($_GET['page'] == "1" || empty($_GET))
{
	//Page one output!
	print "Page 1 / Empty";
}
}

 

Now, I know there's something wrong here, in the fact that if I run the script without passing a GET variable, I get a warning/error, because $_GET['page'] isn't set. How can I get around this? Is there no way to supply results for both scenarios?

 

Thanks

 

FlyingIsFun1217

Link to comment
Share on other sites

if (isset($_GET)) {
   if (!array_key_exists('page', $_GET) && !empty($_GET))
{
	//Put in template code for error page
	print 'Wrong GET name!';
}
else
{
//Page 1 - Welcome, process explanation
if($_GET['page'] == "1" || empty($_GET))
{
	//Page one output!
	print "Page 1 / Empty";
}
}
}
else {
   // Do this when get is not present.
}

Link to comment
Share on other sites

Basically, the problem I have with any of these proposed solutions is that an error is thrown when I run the script and don't supply a GET variable in the URL (which is a possible case in real-world use). When the PHP interpereter sees that I am referencing $_GET['post'], and it has nothing in that GET array, it freaks out and throws me an error. Is this maybe just my server/PHP setup?

 

FlyingIsFun1217

Link to comment
Share on other sites

I didn't read into the question very much. You're getting the error because your checking GET. I thing GET is going to exist no matter what. I'd have to check that though. You need to do an empty check for the specific variable.

 

if (empty($_GET['page'])) {

 

Edit:

 

Just checked. POST ans GET always exist. (At least in my case.) If you wanted to see if any variables were passed, you could do this:

 

if (count($_GET) == 0) {
   // No variables
}

Link to comment
Share on other sites

error_reporting(-1);
ini_set('display_errors', '1');

if (!array_key_exists('page', $_GET) && !empty($_GET)){
echo 'foo';
} else {
echo 'bar';
}

 

Your logic for checking is odd though. You probably want this instead

 

error_reporting(-1);
ini_set('display_errors', '1');

if ( !isset($_GET['page']) ) {
	//Put in template code for error page
	print 'Page does not exist';
} else {
//Page 1 - Welcome, process explanation
if( $_GET['page'] == "1" || empty($_GET['page']) ) {
	//Page one output!
	print "Page 1 / Empty";
} else {
	print "Page is {$_GET['page']}";
}
}

 

Gives me no errors with no query string, with ?page= and with ?page=foobar

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.