0

I can't figure out why this script isn't working.

<?php

if (($_GET['p'] != 'index') && 
    ($_GET['p'] != 'reg') && 
    ($_GET['p'] != 'login') && 
    ($_GET['p'] != 'ad') && 
    (!isset($_GET['p']))):
?>

<?php endif; ?>

I want to not display the error page if the $_GET is not set, which in my experience (!isset($_GET['p'])) should do.

5
  • 3
    Really? The R-word? Is this third grade? Commented Sep 25, 2011 at 18:41
  • You might want to rephrase your title to provide a more accurate description of the problem. A better description will attract more and better answers ! Commented Sep 25, 2011 at 18:42
  • If you're wanting to check an !isset($_GET['p']), put it first in the list BEFORE checking equality. Commented Sep 25, 2011 at 18:44
  • Also, I don't know why you have so many parentheticals grouping single conditions. They're superfluous to what is happening. Commented Sep 25, 2011 at 18:47
  • By "$_GET is not set" Do you mean you want !isset($_GET['p']) to evaluate to TRUE when the get request is like this ? or when it is ?p=? Commented Sep 25, 2011 at 19:01

4 Answers 4

5

If $_GET['p'] is not set, you can't check $_GET['p'] != 'index' and all the others. You'll have to check if it's set first:

<?php if(
    ! isset( $_GET['p'] ) ||
    ($_GET['p'] != 'index' &&
    $_GET['p'] != 'reg' &&
    $_GET['p'] != 'login' &&
    $_GET['p'] != 'ad')
): ?>

A better solution would be to put all those values in an array, and check if $_GET['p'] is in the array:

<?php if(
    ! isset( $_GET['p'] ) ||
    ! in_array(
        $_GET['p'],
        array('index', 'reg', 'login', 'ad')
    )
): ?>

EDIT:

Now that you provided some more info, here's what you should do:

if ( ! isset($_GET['p']) )
{
    // We're at the index page, so don't display anything
}
else
{
    if ( in_array( $_GET['p'], array('index', 'reg', 'login', 'ad') ) )
    {
        // Display your content window
    }
    else
    {
        // $_GET['p'] is not a valid value, display error
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

i copied your code straight from your comment and it still doesn't work, i even did arrays before and it just doesn't work, It does work when i check if the GET is a certain value, but not if it's not set at all
I don't understand - if you check if $_GET['p'] is null or empty, why you need other conditions to check values from that variable? If $_GET['p'] is null or empty then it doesn't contain values...
@evilone - As far as I understand the OP, this is what he's trying to do: If no value was passed as $_GET['p'], or $_GET['p'] is not equals to any of those values, then he glides through. Otherwise, he wants to issue an error.
I have a page where it checks if a $_GET is set with a certain value, for example if it's Hello then it displays a content window with the text "Hello". But i want nothing to be displayed on the Index, where no $_GET is set, and the Error page to be displayed when the $_GET value does not match any set value.
@JosephSilber Yeah. that is what i want.
|
4

Your condition makes no sense. You're checking for 3 possible values of $_GET['p'] and then checking if $_GET['p'] is even set. Reverse your logic:

<?php
    if(isset($_GET['p']))
    {
        // display error page
    }
    else
    {
        // do something else
    }

Comments

2

You can check if $_GET['p'] is set by

if(isset($_GET['p']) {...}

If it's set and not empty then you can check for values that you need to check.

2 Comments

i have tried !$_GET['p'], !isset, setting a variable when it's set, putting it in a different if statement both before And after the long list of if's, and Nothing seems to work.. the get looks like this in the address bar: ?p=index, and i just can't seem to get it to work
@albinlennstrand Are you sure that $_GET['p'] is empty when you think it's empty?
0

Try this:

<?php 
$defaultvalue=0; // for example 0
$p=isset($_GET["p"]) ? $_GET["p"] : $defaultvalue;
if(($p != 'index') && ($p != 'reg') && ($p != 'login') && ($p != 'ad')): 
?>

1 Comment

worked just like the old script, it works if the certain values are set but not if it's not set at all :( i appreciate your help tho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.