New to PHP

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
zeon82
Forum Newbie
Posts: 3
Joined: Wed Mar 25, 2009 10:33 am

New to PHP

Post 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:
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: New to PHP

Post 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;
 
zeon82
Forum Newbie
Posts: 3
Joined: Wed Mar 25, 2009 10:33 am

Re: New to PHP

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: New to PHP

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: New to PHP

Post by pickle »

Another important change was the alteration of the || condition to && .
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply