Page 1 of 1

Problem with exit() and Firefox?

Posted: Thu Jan 20, 2011 2:21 pm
by darylnagode
Hello,

I am new to PHP, just been learning over the last few weeks. I have made a slightly modified version of a script in the book PHP6 and MySQL 5 by Larry Ullman. It is a page that is intended to register new users. Everything works fine except if there is an error when actually applying the query, I want to terminate the script and leave what I have already output on there, if that makes any sense. I intentionally put a mis-spelling in there to trigger an error (birst_name).This works properly in Chrome, but in Firefox it displays as it should briefly, then almost instantly refreshes the page. Is this a bug, or is there another way to do this that will be more consistent across browsers? Also, if anyone else sees any other issues such as security risks in my code, please let me know. I would probably do some more strict filtering of the form entries, but I don't really know what to filter out at this point. I'm sure it will be covered more further in this book.

Code: Select all

<?php

	$page_title = 'Register';
	include_once ('includes/header.html');
	
	// Check to see if a form has been submitted yet
	if (isset($_POST['submitted'])) {
		
		// The user has submitted a form, check to see if all items are proper
		
		// Initialize an array to store all problems that arise
		$errors = array();
		
		if (empty($_POST['first_name'])) {
			$errors[] = 'You did not enter a first name.';
			$first_name = NULL;
		} else {
			$first_name = trim($_POST['first_name']);
		}
		
		if (empty($_POST['last_name'])) {
			$errors[] = 'You did not enter a last name.';
			$last_name = NULL;
		} else {
			$last_name = trim($_POST['last_name']);
		}
		
		if (empty($_POST['email'])) {
			$errors[] = 'You did not enter an email address.';
			$email = NULL;
		} else {
			$email = trim($_POST['email']);
		}
		
		if (empty($_POST['pass1'])) {
			$errors[] = 'You did not enter a password.';
			$pass1 = NULL;
		} else {
			$pass1 = trim($_POST['pass1']);
		}
		
		if (empty($_POST['pass2'])) {
			$errors[] = 'You did not confirm your password.';
			$pass2 = NULL;
		} else {
			$pass2 = trim($_POST['pass2']);
		}
		
		// They entered 2 passwords, check and make sure they match
		if ($pass1 && $pass2) {
			if ($pass1 != $pass2) {
				$errors[] = 'Your passwords did not match.';
				$pass1 = NULL;
				$pass2 = NULL;
			} else {
				$pass = $pass1;
			}
		}
		
		// If there were no errors in the submission, register the user in the database
		if (empty($errors)) {
			
			// Connect to the database
			require_once('../mysqli_connect.php');
			
			// Put together the query
			$query = "INSERT INTO users 
			(birst_name, last_name, email, pass, registration_date) VALUES 
			('$first_name', '$last_name', '$email', SHA1('$pass'), NOW() )";
			
			// Run the query
			$result = @mysqli_query($dbc, $query);
			
			// If the query ran successfully
			if ($result) {
				echo '
					<h1>Thank you!</h1>
					<p>
					You are now registered.
					</p>
					<p><br /></p>
				';
			}
			// There was a problem with the query
			else {
				echo '
					There was a system error while trying to register you. This error has been logged and the site manager has been notified. The issue will be resolved as quickly as possible. We apologize for the inconvenience.
				';
				
				$error_message = 'MySQLi Error: ' . mysqli_error($dbc) 
					. '\n Query: ' . $query;
					
				error_log($error_message, 3, 'error_log.txt');
				
				// notify somebody?
			}
			
			// Whether or not the submission was successful, we now want to close the 
			// database connection and prevent the form from showing again
			
			mysqli_close($dbc);
			include_once('includes/footer.html');
			exit();
		} 
		// There were errors in the user's form, display the errors and the form again
		else {
			echo '
				<h1>Error!</h1>
				<p class="error">
				The following error(s) occurred:<br />';
				foreach ($errors as $e) {
					echo " - $e<br />\n";
			}
			echo '
				</p>
				<p>
				Please try again.
				</p>
				<p><br /></p>
			';
		}
		
	} // end if (isset($_POST['submitted']))

	// The user has not submitted a form, or had errors in their submission
	// display form and populate with any valid values they entered previously
?>

<form action="" method="post">

	<p>First Name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php  if(isset($first_name)) {echo $first_name;} ?>" /></p>
	
	<p>Last Name:<input type="text" name="last_name" size="15" maxlength="40" value="<?php if (isset($last_name)) {echo $last_name;} ?>" /></p>
	
	<p>Email Address: <input type="text" name="email" size="20" maxlength="80" value="<?php if (isset($email)) {echo $email;} ?>" /></p>
	
	<p>Password: <input type="password" name="pass1" size="10" maxlength="20" value="<?php if (isset($pass1)) {echo $pass1;} ?>"/></p>
	
	<p>Confirm Password: <input  type = "password" name="pass2" size="10" maxlength="20" value="<?php if (isset($pass2)) {echo $pass2;} ?>" />
	
	<p><input type="submit" name="submit" value="Register" /></p>
	
	<p><input type="hidden" name="submitted" value="TRUE" /></p>

</form>

<?php

	include_once('includes/footer.html');

?>


Re: Problem with exit() and Firefox?

Posted: Thu Jan 20, 2011 2:27 pm
by darylnagode
I tested again and it works properly in Firefox when a submission is successful, which is using the same 3 lines of code when terminating the script, so it doesn't make much sense to me.

When I enter a successful submission after fixing the typo, the page that says Thank you! You are now registered stays there.

As soon as that is output to the browser, this code is executed:

mysqli_close($dbc);
include_once('includes/footer.html');
exit();

However the system error message should also use those exact same 3 lines of code. Is it something to do with the error logging that is causing a problem?

Re: Problem with exit() and Firefox?

Posted: Thu Jan 20, 2011 2:31 pm
by darylnagode
To clarify, both messages (success and failure) and the script exiting work fine in Google Chrome. In firefox, the success message stays like it should, but when the system error message is displayed, the page refreshes instantly, which just shows the registration form again. I had to do a screen capture to even see that it was being printed to the browser correctly.

Re: Problem with exit() and Firefox?

Posted: Thu Jan 20, 2011 2:34 pm
by John Cartwright
Can you post the error_log() function, and perhaps the footer. There is nothing in your code that I can see that will cause the browser to refresh. It has to be something else.

P.S., PHP6 doesn't exist, and won't exist for a long time (they stopped development on it in favor of continuing 5.3+).

And please, make use of the edit button when updating your posts with new information. This is considered bumping, which we try to keep it down to 1 per day.

Re: Problem with exit() and Firefox?

Posted: Thu Jan 20, 2011 3:02 pm
by darylnagode
My apologies about the bumping thing I didn't mean to. I just figured it out. I have XRefresh set up with Firebug, a developer's extension for firefox. It is set up so that whatever page(s) XRefresh is connected to will automatically refresh in firefox whenever changes are saved in a particular monitored folder. This is very handy when you're trying to set up CSS on a webpage, but not so handy when the error log file is being created in that folder and triggering the refresh on the page. I didn't have Firebug open, but I guess maybe since I used it last with the same host (localhost) Xrefresh was still attached to it somehow. Thanks anyway!!

Re: Problem with exit() and Firefox?

Posted: Thu Jan 20, 2011 3:07 pm
by John Cartwright
Interesting feature. I didn't know Firebug had that.

Glad your sorted.

Re: Problem with exit() and Firefox?

Posted: Fri Jan 21, 2011 6:44 am
by Weirdan
John Cartwright wrote:Interesting feature. I didn't know Firebug had that.
XRefresh is a separate extension, it's not a feature of Firebug.

John Cartwright wrote:Can you post the error_log() function, and perhaps the footer.
error_log() is a built-in php function: http://us2.php.net/manual/en/function.error-log.php