I am running enclosed script with 3 lines I
inserted to report where errors occur:
echo "errors to line 204", count($errors);
echo "errors to line 266", count($errors);
When I submit a form from another script, it submits to enclosed script
and I get:
errors to ine 204 0
errors to line 266 1
The block of code in between is an email validation.
Then the screen just stops.
How can I fix ?
<?php
// This script validates customer data entered into
// example.customer.2.php.
// If validation succeeds, it INSERTs or UPDATEs
// a customer and redirect to a receipt page; if it
// fails, it creates error messages and these are later
// displayed by example.customer.2.php.
include 'include.inc';
set_error_handler("errorHandler");
// Initialize a session
session_start();
// Register an error array - just in case!
if (!session_is_registered("errors"))
session_register("errors");
// Clear any errors that might have been
// found previously
$errors = array();
// Set up a $formVars array with the POST variables
// and register with the session.
if (!session_is_registered("formVars"))
session_register("formVars");
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname] = trim(clean($value, 50));
// Validate the firstName
if (empty($formVars["firstName"]))
// First name cannot be a null string
$errors["firstName"] =
"The first name field cannot be blank.";
elseif (!eregi("[a-z'-]*$", $formVars["firstName"]))
// First name cannot contain white space
$errors["firstName"] =
"The first name can only contain alphabetic " .
"characters or \"-\" or \"'\"";
elseif (strlen($formVars["firstName"]) > 50)
$errors["firstName"] =
"The first name can be no longer than 50 " .
"characters";
// Validate the Surname
if (empty($formVars["surname"]))
// the user's surname cannot be a null string
$errors["surname"] =
"The surname field cannot be blank.";
elseif (strlen($formVars["surname"]) > 50)
$errors["surname"] =
"The surname can be no longer than 50 " .
"characters";
// Validate the Address
if (empty($formVars["address1"]) &&
empty($formVars["address2"]) &&
empty($formVars["address3"]))
// all the fields of the address cannot be null
$errors["address"] =
"You must supply at least one address line.";
else
{
if (strlen($formVars["address1"]) > 50)
$errors["address1"] =
"The address line 1 can be no longer " .
"than 50 characters";
if (strlen($formVars["address2"]) > 50)
$errors["address2"] =
"The address line 2 can be no longer " .
"than 50 characters";
if (strlen($formVars["address3"]) > 50)
$errors["address3"] =
"The address line 3 can be no longer " .
"than 50 characters";
}
// Validate the user's Initial
if (!empty($formVars["initial"]) &&
!eregi("[a-z]{1}$", $formVars["initial"]))
// If there is a middle initial, it must be
// one character in length
$errors["initial"] =
"The initial field must be empty or one " .
"character in length.";
// Validate the City
if (empty($formVars["city"]))
// the user's city cannot be a null string
$errors["city"] = "You must supply a city.";
elseif (strlen($formVars["city"]) > 20)
$errors["city"] =
"The city can be no longer than 20 characters";
// Validate State - any string less than 21 characters
if (strlen($formVars["state"]) > 20)
$errors["state"] =
"The state can be no longer than 20 characters";
// Validate Zipcode
if (!ereg("([0-9]{4,5})$", $formVars["zipcode"]))
$errors["zipcode"] =
"The zipcode must be 4 or 5 digits in length";
// Validate Country
if (strlen($formVars["country"]) > 20)
$errors["country"] =
"The country can be no longer than 20 characters";
// Phone is optional, but if it is entered it must have
// correct format
$validPhoneExpr =
"([0-9]{2,3}[ ]?)?[0-9]{4}[ ]?[0-9]{4}$";
if (!empty($formVars["phone"]) &&
!ereg($validPhoneExpr, $formVars["phone"]))
$errors["phone"] =
"The phone number must be 8 digits in length, " .
"with an optional 2 or 3 digit area code";
// Fax is optional, but if it is entered it must
// have correct format
if (!empty($formVars["fax"]) &&
!ereg($validPhoneExpr, $formVars["fax"]))
$errors["fax"] =
"The fax number must be 8 digits in length, with " .
"an optional 2 or 3 digit area code";
// Validate Date of Birth
if (empty($formVars["dob"]))
// the user's date of birth cannot be a null string
$errors["dob"] = "You must supply a date of birth.";
elseif (!ereg("([0-9]{2})/([0-9]{2})/([0-9]{4})$",
$formVars["dob"], $parts))
// Check the format
$errors["dob"] =
"The date of birth is not a valid date in the " .
"format DD/MM/YYYY";
elseif (!checkdate($parts[2],$parts[1],$parts[3]))
$errors["dob"] =
"The date of birth is invalid. Please check " .
"that the month is between 1 and 12, and the " .
"day is valid for that month.";
elseif (intval($parts[3]) < 1890)
// Make sure that the user has a reasonable birth year
$errors["dob"] =
"You must be alive to use this service.";
elseif
// Check whether the user is 18 years old.
// If all the following are NOT true, then report
// an error.
// Were they born more than 19 years ago?
(!((intval($parts[3]) < (intval(date("Y") - 19))) ||
// No, so were they born exactly 18 years ago, and
// has the month they were born in passed?
(intval($parts[3]) == (intval(date("Y")) - 18) &&
(intval($parts[2]) < intval(date("m")))) ||
// No, so were they born exactly 18 years ago in this
// month, and was the day today or earlier in the month?
(intval($parts[3]) == (intval(date("Y")) - 18) &&
(intval($parts[2]) == intval(date("m"))) &&
(intval($parts[1]) <= intval(date("d"))))))
$errors["dob"] =
"You must be 18+ years of age to use this ".
"service.";
// Only validate email if this is an INSERT
echo "errors to line 204", count($errors);
if (!session_is_registered("loginUsername"))
{
// Check syntax
$validEmailExpr =
"0-9a-z~!#$%&-" .
"@0-9a-z~!#$%&-$";
// Extract the domain of the email address
$maildomain = substr(strstr($formVars["email"], '@'), 1);
if (empty($formVars["email"]))
// the user's email cannot be a null string
$errors["email"] =
"You must supply an email address.";
elseif (!eregi($validEmailExpr, $formVars["email"]))
// The email must match the above regular
// expression
$errors["email"] =
"The email address must be in the " .
"name@domain format.";
elseif (strlen($formVars["email"]) > 50)
// The length cannot exceed 50 characters
$errors["email"] =
"The email address can be no longer than " .
"50 characters.";
else
{
// Check if the email address is already in use in
// the winestore
if (!($connection = @ mysql_pconnect($hostName,
$username,
$password)))
showerror();
if (!mysql_select_db($databaseName, $connection))
showerror();
$query = "SELECT * FROM users
WHERE user_name = '" .
$formVars["email"] . "'";
if (!($result = @ mysql_query ($query,
$connection)))
showerror();
// Is it taken?
if (mysql_num_rows($result) == 1)
$errors["email"] =
"A customer already exists with this " .
"login name.";
}
}
// Only validate password if this is an INSERT
// Validate password - between 6 and 8 characters
echo "errors to line 266", count($errors);
if (!session_is_registered("loginUsername") &&
(strlen($formVars["loginPassword"]) < 6 ||
strlen($formVars["loginPassword"] > 8)))
$errors["loginPassword"] =
"The password must be between 6 and 8 " .
"characters in length";
echo "272";
echo count($errors);
// Now the script has finished the validation,
// check if there were any errors
if (count($errors) > 0)
{
// There are errors. Relocate back to the client form