[SOLVED] HELP NEEDED - Comparison operator not working

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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

[SOLVED] HELP NEEDED - Comparison operator not working

Post by RobertGonzalez »

I am trying to force a page to load only when a certain query string is valid and only when the passed value is a certain value. I am using an if statement which works almost completely. The problem I am getting is that the value '0' (the number zero) should not be allowed to load the page. But when I enter it in the query string, the page loads anyway. All other 'unacceptable' values are kicked out and the result is what you would expect when looking at the code. Can someone please tell me what I am doing wrong? Thanks for all your help.

Code: Select all

<?php
if ( !(isset($_GET['dealer'])) || ($_GET['dealer'] >= 3) || ($_GET['dealer'] == 0) || !(is_numeric($_GET['dealer'])) )
{
	header("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "dealers.php");
	exit();
}
?>
PS I have tried changing the comparison on the 'zero' issue. Using greater thans, less thans or equals to has yielded the same result.
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

if ( !(isset($_GET['dealer'])) || ($_GET['dealer'] >= 3) && ($_GET['dealer'] != 0) || !(is_numeric($_GET['dealer'])) )
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

precedence may make that run a bit odd, Bastien.

Code: Select all

if ( !isset($_GET&#1111;'dealer']) || !is_numeric($_GET&#1111;'dealer']) || (($_GET&#1111;'dealer'] >= 3) && ($_GET&#1111;'dealer'] != 0)) )
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

yeah, it seemed a little screwy...maybe if i had some idea of what the acceptable values are?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Why not...

Code: Select all

if ( empty($_GET['dealer']) or !is_numeric($_GET['dealer']) or abs($_GET['dealer']) > 2 )
The 0 (not allowed) is the same as FALSE, so empty() will trigger it. Added abs() to get rid of the possible =-1 idea some people might have...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Thanks Jam, that snippet worked out perfectly. I appreciate everyone's help.
Post Reply