Header() function problems

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
rmmoul
Forum Newbie
Posts: 1
Joined: Thu Dec 02, 2010 4:35 am

Header() function problems

Post by rmmoul »

I'm writing a script for registering new users, and when the form is submitted it goes to my form handler php script, if there is an error, the php script should redirect the user back the registration form with a message about the error. I'd like to use the header() function to do the redirection, but the code is acting in a way that i can't understand. When header() is called in an if statement that is true, it skips the header() function in the if statement and executes the one outside of the if statement. At first I thought my code was funky, but i broke it down to the simplest example:

Code: Select all

	
	$error = '1';
	
	if($error == '1')
	{
		
		header('Location: reg.php');
	}
	
	header('Location: success.php');

This goes to success everytime, even though $error equals 1... I couldn't find a solution or explanation anywhere.

also,

Code: Select all


$error = '1';

	if($error == '1')
	{
		
		header('Location: 1.php');
	}
	
	if($error == '1')
	{
		
		header('Location: 2.php');
	}
	
	if($error == '1')
	{
		
		header('Location: 3.php');
	}

that code will go to 3.php. I'm stumped. If I can't redirect with php in this manner then how should i redirect my pages? I like the header() function because when you use the back button on the browser is skips the script and goes back to the last loaded html page.
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Header() function problems

Post by curlybracket »

Manual says:

Code: Select all

header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
So i guess you will need those exit; to terminate the script.
Post Reply