<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>BBK ITApps - Web Programming using PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<?php

/**
* @author Tobi Brodie
* @copyright 2011
*/

function check_name($name){
//
$name_is_valid = true;
$str_length = strlen($name);
if($str_length > 1 && $str_length < 100){
$name_array = explode(' ',$name);
foreach($name_array as $name_from_array){
if(stristr($name_from_array,'\'')){
$another_name_array = explode('\'',$name_from_array);
if(count($another_name_array)==2){
foreach($another_name_array as $name_split_again){
if(!ctype_alnum($name_split_again)){
$name_is_valid = false;
}
}
}
else{
$name_is_valid = false;
}
}
elseif (!ctype_alpha($name_from_array)){
$name_is_valid = false;
}
}
}
else {
$name_is_valid = false;
}
return $name_is_valid;
}

function check_email($email){
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex){
$isValid = false;
}
else{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64){
// local part length exceeded
$isValid = false;
}
else if($domainLen < 1 || $domainLen > 255){
// domain part length exceeded
$isValid = false;
}
else if($local[0] == '.' || $local[$localLen-1] == '.'){
// local part starts or ends with '.'
$isValid = false;
}
else if(preg_match('/\\.\\./', $local)){
// local part has two consecutive dots
$isValid = false;
}
else if(!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)){
// character not valid in domain part
$isValid = false;
}
else if(preg_match('/\\.\\./', $domain)){
// domain part has two consecutive dots
$isValid = false;
}
else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',str_replace("\\\\","",$local))) {
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local))){
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))){
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}

 

/* First we check to see if this is the first time the page is called,
in which case we want to just show the form, otherwise we need to validate the data
and then decide if we redisplay the form.
We check this by testing if the form has been submitted */

if (isset($_POST['submit'])) {
// form has been submitted so we validate form

// we start by initialising a few variables
$errors = 0;
$errmsg = '';
$clean = array();

// we then check the inputs one by one
// name check

if(isset($_POST['name'])){
if (check_name(trim($_POST['name']))) {
$clean['name'] = $_POST['name'];
}
else {
$errors++;
$errmsg .= '<p>Your name contains invalid characters. Please enter just letters.</p>';
}
}
else {
$errors++;
$errmsg .= '<p>Your name is required, please enter your name below.<p>';
}

// email check

if(isset($_POST['email'])){
if (check_email(trim($_POST['email']))) {
$clean['email'] = $_POST['email'];
}
else {
$errors++;
$errmsg .= '<p>Your email address is not valid. Please enter a valid email address below.</p>';
}
}
else {
$errors++;
$errmsg .= '<p>Your email address is required, please enter a valid email address below.<p>';
}

}

 

if (isset($_POST['submit']) && $errors === 0) {
// action data
echo '<p>Thank you for submitting your email!</p>';
}
else {
// if (re)displaying form: print error message
if ($errors > 0) {
echo '
<p>There is a problem with the form you submitted.</p>
<p>The following errors were found:</p>'.
$errmsg;
}
// (re)display form

?>
<h2>Join my mailing list for PHP tips and treats!</h2>
<form action="<?php echo $_SERVER['file:///C|/Users/ubtbro01/Desktop/SCRIPT_NAME']; ?>" method="post">
<fieldset>
<legend>Contact details</legend>
<!-- Name -->
<p>
<label for="name">Name:</label>
<input name="name" size="40" id="name" type="text"
value="<?php echo (isset($clean['name'])?htmlentities($clean['name']) :''); ?>" />
</p>
<!-- Email -->
<p>
<label for="email">Email:</label>
<input name="email" size="40" id="email" type="text"
value="<?php echo (isset($clean['email'])?htmlentities($clean['email']) :''); ?>" />
</p>
<!-- Submit button -->
<p><input type="submit" value="submit" name="submit" /></p>
</fieldset>
</form>
<?php

}

?>
</body>
</html>