Page 1 of 1

New to PHP

Posted: Wed Mar 25, 2009 10:48 am
by zeon82
Hi

First off i just want to point out i am quite new to php. I have a simple html form that has a text box called username and a submit button.When the submit button is pressed it actions a piece of php code.

Code: Select all

<?PHP
 
$username = $_POST['username'];
$redirect_page='http://www.mydomain.com/prices.htm';
$redirect_page2='http://www.mydomain.com/prices2.htm';
$redirect_page3='http://www.mydomain.com/prices3.htm';
 
    if ($username >= "0" || $username <= "150000") {
    header('Location: '.$redirect_page);
    }
    elseif ($username > "150000" || $username <= "250000") {
    header('Location: '.$redirect_page2);
    }
    elseif ($username >= "250000") {
    header('Location: '.$redirect_page3);
    }
exit;
 
?>
What i want to do is,

if the user types in a number greater than or equal to 15000 or less than or equal to 150000, then they need to be directed to redirect page.
if the user types in a number greater than 150000 or less than or equal to 250000, then they need to be directed to redirect page2.
if the user types in a number greater than 250000, then they need to be directed to redirect page3.

For some reason i keep getting redirected to redirect page, not the relevant one for the numbers entered.

Apologies if this is really simple and im missing something obvious. Any help will be much appreciated i have been trawling the web for ages without much luck :banghead:

Re: New to PHP

Posted: Wed Mar 25, 2009 11:09 am
by Mark Baker

Code: Select all

 
 
$username = (int) $_POST['username'];    // cast to a numeric value
$redirect_page='http://www.mydomain.com/prices.htm';
$redirect_page2='http://www.mydomain.com/prices2.htm';
$redirect_page3='http://www.mydomain.com/prices3.htm';
 
if ($username >= 0 && $username <= 150000) {    //compare against numerics, and use && rather than ||
    header('Location: '.$redirect_page);
    exit;
} elseif ($username > 150000 && $username <= 250000) {
    header('Location: '.$redirect_page2);
    exit;
} elseif ($username > 250000) {
    header('Location: '.$redirect_page3);
    exit;
}
What happens here?
exit;
 

Re: New to PHP

Posted: Wed Mar 25, 2009 12:15 pm
by zeon82
many thanks for that, it works a treat.

I put the exit statement at the end because i thought that was where it needed to be, and not one for each If statement. But now i know, that is just down to inexperience. :D

Re: New to PHP

Posted: Wed Mar 25, 2009 12:35 pm
by Mark Baker
zeon82 wrote:many thanks for that, it works a treat.

I put the exit statement at the end because i thought that was where it needed to be, and not one for each If statement. But now i know, that is just down to inexperience. :D
You could get away with the exit statement at the end rather than for each individual redirect.
However, you have no checking in case the user puts in other values such as -1 that will drop through the set of if statements, and then exit. Do you simply give them a blank page, or do you echo an error message telling them what they've done wrong.

If the latter, then you need the exit after each redirect, otherwise the flow of the code may display that error message even for valid values of $username before doing the redirect

Re: New to PHP

Posted: Wed Mar 25, 2009 12:37 pm
by pickle
Another important change was the alteration of the || condition to && .