<?php
// This script validates customer data entered
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($_POST 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 can 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 thatn 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 a blank.";
elseif (strlen($formVars["surname"]) > 50)
// the user's surname cannot be a null string
$errors["surname"] = " The surname can be no longer than 50 charaters.";
// Validate the address
if (empty($formVars["address1"]) && (empty($formVars["address2"])))
// the user's address cannot be a null string
$errors["address"] = " You must supply an address";
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";
}
// Valadate the city
if (empty($formVars["city"]))
// the users 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 charaters";
// Validate state
if (empty($formVars["state"]))
// the users state cannot be a null string
$errors["state"] = "You Must Supply a State";
elseif (strlen($formVars["state"]) > 20)
$errors["state"] = "The State Can be no longer than 20 charaters";
// Validate Zip Code
if(!ereg("^([0-9]{4,5})$", $formVars["zipcode"]))
$errors["zipcode"] = "The zipcode must be 4 or 5 digits in length";
// Validate Country
if (empty($formVars["country"]))
// the users country cannot be a null string
$errors["country"] = "You Must Supply a Country";
elseif (strlen($formVars["country"]) > 20)
$errors["country"] = "The Country Can be no longer than 20 charaters";
// Validate Phone
$validPhoneExpr = "^([0-9]{2,3}[ ]?)?[0-9]{3}[ ]?[0-9]{4}$";
if (empty($formVars["phone"]))
// the users phone cannot be a null string
$errors["phone"] = "You Must Supply a Phone Number";
elseif (!empty($formVars["phone"]) && !ereg($validPhoneExpr, $formVars["phone"]))
// Correct phone Format
$errors["phone"] ="The Phone Number Must Be in this format \"123 456 7890\", including your area code";
// Fax is optional
if (!empty($formVars["fax"]) && !ereg($validPhoneExpr, $formVars["fax"]))
// Correct fax Format
$errors["fax"] ="The fax Number Must Be in this format \"123 456 7890\", including your area code";
// Valid Date Of Birth
if(empty($formVars["dob"]))
$errors["dob"] = "You Must Supply A Birth Date in the" . "format DD/MM/YYYY";
elseif (!ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", $formVars["dob"], $parts))
// Check the format
$errors["dob"] = "The data 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 the 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 if user is 18
(!((intval($parts[3]) < (intval(date("Y") - 19))) || (intval($parts[3]) == (intval(date("Y")) - 18) && (intval($parts[3]) < intval(date("m")))) || (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 valid E-mail if this is an INSERT
if(!session_is_registered("loginUsername"))
{
// check syntax
$validEmailExpr = "^[0-9a-z~`!#$%&_-]([.]?[0-9a-z~!#$%&_-])*" . "@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*$";
if(empty($formVars["email"]))
// You must Supply an e-mail
$errors["email"] = "You Must Supply an e-mail address";
elseif (!eregi($validEmailExpr, $formVars["email"]))
// The email must match the above regular expression
$errors["email"] = "The e-mail address must be in the [email]jon@doe.com[/email] format.";
elseif (strlen($formVars["email"]) > 50)
// the length cannot excced 50 charaters
$errors["email"] = "The e-mail address can be no longer than 50 charaters.";
/* elseif (!(getmxrr(substr(strstr($formVars["email"], '@'), 1), $temp)) || checkdnsrr(gethostbyname(substr(strstr($formVars["email"], '@'), 1)),"ANY"))
$errors["email"] = "the domain does not exist."; */
else
{
// Check if the e-mail address is allready in use
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 Taken ??
if (mysql_num_rows($result) == 1)
$errors["email"] = "A Customer Already exisits with this" . " Login Name";
}
}
// Only Validate password if this is an INSERT
// Validate Password - between 8 and 15 characters
if (!session_is_registered("loginUsername") && (strlen($formVars["loginPassword"]) < 8 || strlen($formVars["loginPassword"] > 15)))
$errors["loginPassword"] = "Your Passwords Must Be Between 8 and 12 charaters in length";
// Now the script has finished the validation, check if there were any errors
if (count($errors) > 0)
{
// There are errors. Relocate back to the costomer form
header("Location: custform.php");
exit;
}
// if we made it here, then the data is valid
if (!isset($connection))
{
if (!($connection = @ mysql_pconnect($hostName, $username, $password)))
showerror();
if (!mysql_select_db($databaseName, $connection))
showerror();
}
// Reassemble the date of birth into database format
$dob = "\"$parts[3]-$parts[2]-$parts[1]\"";
// Is this an update
if (session_is_registered("loginUsername"))
{
$custID = getCustomerID($loginUsername, $connection);
$query = "UPDATE customer SET " . "title = \"" . $formVars["title"] . "\", " .
"surname = \"" . $formVars["surname"] . "\", " .
"firstname = \"" . $formVars["firstName"] . "\", " .
"addressline1 = \"" . $formVars["address1"] . "\", " .
"addressline2 = \"" . $formVars["address2"] . "\", " .
"city = \"" . $formVars["city"] . "\", " .
"state = \"" . $formVars["state"] . "\", " .
"zipcode = \"" . $formVars["zipcode"] . "\", " .
"country = \"" . $formVars["country"] . "\", " .
"phone = \"" . $formVars["phone"] . "\", " .
"fax = \"" . $formVars["fax"] . "\", " .
"birth_date = " . $dob .
" WHERE cust_id = $custID";
}
else
$query = "INSERT INTO customer VALUES (" .
"\"" . $formVars["surname"] . "\", " .
"\"" . $formVars["firstName"] . "\", " .
"\"" . $formVars["title"] . "\", " .
"\"" . $formVars["address1"] . "\", " .
"\"" . $formVars["address2"] . "\", " .
"\"" . $formVars["city"] . "\", " .
"\"" . $formVars["state"] . "\", " .
"\"" . $formVars["zipcode"] . "\", " .
"\"" . $formVars["country"] . "\", " .
"\"" . $formVars["phone"] . "\", " .
"\"" . $formVars["fax"] . "\", " .
"\"" . $formVars["email"] . "\", " .
$dob . ", " . 0 . ")";
// Run The Query On the customer table
if (!(@ mysql_query ($query, $connection)))
showerror();
// If this was an INSERT, we need to INSERT
if (!session_is_registered("loginUsername"))
{
// Get the cutomer id that was created
$custID = @ mysql_insert_id($connection);
// Use the first two characters of the email address, as a salt for the passwd
$salt = substr($formVars["email"], 0, 2);
// Create the encrypted password
$stored_password = crypt($formVars["loginPassword"], $salt);
// INSERT a new user into the user table
$query = "INSERT INTO users SET cust_id = $custID, password = '$stored_password', user_name = '" . $formVars["email"] . "'";
if (!($result = @ mysql_query ($query, $connection)))
showerror();
// Log the user into their new account
session_register("loginUsername");
$loginUsername = $formVars["email"];
}
// Clear the FormVars so a future <form> is blank
session_unregister("formVars");
session_unregister("errors");
// Now Show the user is a member
echo "Welcome New Member";
?>