Forum Moderators: coopster

Message Too Old, No Replies

Using STRPOS in an IF Statement

Can't seem to get a correct read.

         

inveni0

2:39 pm on Apr 3, 2009 (gmt 0)

10+ Year Member



I'm trying this code:


if(strpos($_POST['filter_zip'], ",") == false) && (is_numeric($_POST['filter_zip']) == false) {
header("Location: error.php?code=135&term=".$_POST['filter_zip']);
}

As you can see, I want it to run only if the supplied POST does not contain a comma and is not numeric. I submit, via a form, the letters "nix", which fit this description, but this IF statement fails to run. Is the reason too obvious for me to see it?

Thanks for any help!

inveni0

2:49 pm on Apr 3, 2009 (gmt 0)

10+ Year Member



I split the IF statement into a nested statement, like this:

[code]
if(strpos($_POST['filter_zip'], ",") === false) {
echo "NO COMMA";
if(is_numeric($_POST['filter_zip']) == false) {
echo "NO NUMBERS";
header("Location: error.php?code=135&term=".$_POST['filter_zip']);
echo "SKIPPED TRANSFER";
}}

{/code]

And I get all of my echos printed out, but the HEADER command doesn't run. What gives?

Frank_Rizzo

3:02 pm on Apr 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



header does not work if you output something before it. There can not be any echo of chars, or spaces.

remove the echos and it should work but make sure there is no code before it which is outputting anything.

inveni0

3:06 pm on Apr 3, 2009 (gmt 0)

10+ Year Member



It doesn't work with the echos removed. Usually, if you output something, the header command throws an error, but in this script, the header command does nothing at all. I've even placed it outside of the IF statement, and it still does nothing.

EDIT: But it does work if I place just this statement in a file of its own. Guess I'd better comb through my script.

EDIT 2: Still stuck. The crazy thing is that, when this header skips, it only causes another error further in the script, triggering another header command that actually DOES work. I'm so lost.

inveni0

3:29 pm on Apr 3, 2009 (gmt 0)

10+ Year Member



AH HA!

Turns out I needed to place an EXIT statement at the end of my condition. Without it, the script continued to execute and reached a new header command before the previous one could complete.

In case anyone else has the same issue, here's my new code:


if(strpos($_POST['filter_zip'], ",") == false) && (is_numeric($_POST['filter_zip']) == false) {
header("Location: error.php?code=135&term=".$_POST['filter_zip']);
exit;
}