Forwarding to another page if a condition isnt met

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
mayh3m
Forum Newbie
Posts: 3
Joined: Wed Mar 12, 2003 5:44 am
Location: South Africa

Forwarding to another page if a condition isnt met

Post by mayh3m »

Another question from a newbie

Please tell me what statement I should use to go to another php page if a condition isnt met in an if statement.

I have used an include, but this includes the stuff on the previous page which I dont want.

Your help is greatly appreciated

Newbie
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can use header():

Code: Select all

header('Location: http://www.mysite.com/someotherpage.php');
Mac
mayh3m
Forum Newbie
Posts: 3
Joined: Wed Mar 12, 2003 5:44 am
Location: South Africa

header doesnt work

Post by mayh3m »

Hi Twigletmac

I tried your suggestion, but it doesnt seem to work: This is what I'm trying to do

$result = mysql_query($query) or
die (mysql_error());
if (mysql_num_rows($result) == 0)
{
header('../helpdesk/newuser/form.php');
}
else

When I run it, it still goes and processes the "else" even if the result is zero. I've tried making the "header" the else part of the statement and the same is true. Please help
net7
Forum Commoner
Posts: 31
Joined: Wed Mar 12, 2003 1:27 pm

Post by net7 »

You have to include "Location"

so it would look like

Code: Select all

header('Location: www.go.com');
exit();
that should work. remember to keep the "Location:" part in there. And for security reasons you can include exit();
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

As net7 said, you do need the 'Location: ' bit and you also need to read the manual page for the header() function:
PHP Manual wrote:Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself.
There's an example on the page of how to do this.

Mac
Post Reply