Having issue with Code but have no clue why

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
User avatar
boo
Forum Commoner
Posts: 42
Joined: Mon Jul 02, 2007 11:30 am
Location: NY

Having issue with Code but have no clue why

Post by boo »

I am creating a php script to check/validate the form input and for some reason it is not working correctly.

Here is my code

Code: Select all

session_start();
session_register ("sessionerrors");
$sessionerrors = '';
$validationfailed ='N';
 
	

	if(empty($HTTP_POST_VARS['name'])){
		$sessionerrors .= 'Name is blank <br>';
		$validationfailed ='Y';
	}
	if(!isset($HTTP_POST_VARS['termsconfirm']) ){
		$sessionerrors .= 'You must check the terms of Use Checkbox<br>';
		$validationfailed ='Y';
	}
	if(empty($HTTP_POST_VARS['emailaddy'])){
		$sessionerrors .= 'Email is blank <br>';
		$validationfailed ='Y';
	}
	if(empty($HTTP_POST_VARS['emailconfirm'])){
		$sessionerrors .= 'Confirm Email is blank <br>';
		$validationfailed ='Y';
	}
	if(!check_email_address($HTTP_POST_VARS['emailaddy'])) {
		$sessionerrors .= 'Invalid Email address <br>';
		$validationfailed ='Y';
	}
	if(!check_email_address($HTTP_POST_VARS['emailconfirm'])) {
		$sessionerrors .= 'Confirm Email address <br>';
		$validationfailed ='Y';
	}

	if($HTTP_POST_VARS['emailconfirm'] != $HTTP_POST_VARS['emailaddy']){
		$sessionerrors .= 'Emails do not match <br>';
		$validationfailed ='Y';
	}
		
	if(empty($HTTP_POST_VARS['password'])){
		$sessionerrors .= 'Password is blank <br>';
		$validationfailed ='Y';
	}
	
	if(empty($HTTP_POST_VARS['passwordconfirm'])){
		$sessionerrors .= 'Verify Password is blank <br>';
		$validationfailed ='Y';
	}

	if($HTTP_POST_VARS['passwordconfirm'] != $HTTP_POST_VARS['password']){
		$sessionerrors .= 'Passwords do not match <br>';
		$validationfailed ='Y';
	}
	//echo $sessionerrors;
	if ($validationfailed =='Y'){ 
   		header('Location: ../error_outside.php'); 
	} 
........
Now let me tell you what I see with the code above. If the user leaves all of the fields blank then they receive all of the needed error messages and they are sent to the outside_error page.

If the user enters a value in the name field then they are not sent to the outside_error page and the processing continues as if there was not any errors.

Now at the end of the code I have the echo currently commented out but if I display that two things happen. One is it displays all of the messages within sessionerrors and the next is I see an error message that tells me there is an error with the header because of the program already sent output to the display (which I would expect because of the echo) but why doesnt the program see the value of 'Y' in validationfailed unless I put that echo there.

Any help would be greatly appreciated.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Put an exit; after the header call. Some servers will not stop execution after the header call.

PS The fact that you get a header already sent error after the echo should tell you that the code is evaluating properly.
User avatar
boo
Forum Commoner
Posts: 42
Joined: Mon Jul 02, 2007 11:30 am
Location: NY

Post by boo »

Worked like a charm thank you very much.

This will be one thing that I dont forget.
Post Reply