This is Krisse's assignment site for LVS course. Instructor Jeramie Risinger.

Week 4

In this week we did read and practice about:

Forms and Data Checking, The Mail Command, Sending An E-mail, Security



Practice Form

Firstname: *

Lastname: *

Phone (us): *

E-mail: *

Comments: *

* = Required field

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="author" content="Kristiina Hillerström" />
<meta name="Description" content="An assignment site" />
<?php
$dname 
"gmail.com"// my domain name
$webasitename "Introduction to PHP"//title of my web site
$copy "&copy; K.Hillerstr&ouml;m 2008"//copyright in footer 
?>
<title><?php echo $websitename?> Week 4</title>
<link href="introphp.css" rel="stylesheet" type="text/css" />
</head>

<body>
<!-- container -->
<div id="container">

<?php

// Read in variables
$firstname stripslashes($_POST["firstname"]);
$lastname stripslashes($_POST["lastname"]);
$phone stripslashes($_POST["phone"]);
$user_mail stripslashes($_POST["user_mail"]);
$comments stripslashes($_POST["comments"]);
 include (
"includes/header.inc");
 include (
"includes/navigation_list.inc");
 include (
"includes/info.inc");
 include (
"includes/info_week4.inc");
 include (
"includes/variables.inc");
 include (
"includes/functions.inc");

 
// Check to see if all data has been entered
if (!empty($firstname) && !empty($lastname) && !empty($phone) && !empty($user_mail) && !empty($comments)) {
    
// check the lenght of $firstname, lastname and $phone
    
if ((strlen($firstname)) > $len_firstname) { // if input > size limit
        
echo "<p class='red'>ERROR! The entered name is too long. Max lengt is 25.
         Please check the firstname field.</p>"
;
        
$firstname "";
        include (
"includes/form.inc");
        include (
"includes/footer.inc");
        die();
      } else if ((
strlen($lastname)) > $len_lastname) { //if input > size limit
        
echo "<p class='red'>ERROR! The entered name is too long. Max lengt is 35.
         Please check the lastname field.</p>"
;
        
$lastname "";
        include (
"includes/form.inc");
        include (
"includes/footer.inc");
        die();
      } else if ((
strlen($phone)) > $len_phone) { //if input > size limit
        
echo "<p class='red'>ERROR! Your phone number is too long. Max lenght is 14.</p>";
        
$phone "";
        include (
"includes/form.inc");
        include (
"includes/footer.inc");
        die();
      }    
/////////////////////////////////////////////////////////////////////////
 // Compine all the data info one variable
 
foreach ( $_POST as $key => $value ) {
    
$postVars .= $value;
 }
// convert variable to lower case letters for easier testing
 
$postVars strtolower($postVars);
 
// This if statement checks to see if certain data is in the
 // $postVars variable and will email an error message if it is.
 // Then the script will die (end).
if (ereg("mime-version",$postVars
|| 
ereg("mime",$postVars) || ereg("bcc",$postVars) || ereg("cc",$postVars)) {
     
mail("$my_email","From Hijack Attempt",
     
"A spam relay was attempted from the Introduction to PHP site and was blocked.",
     
"From:SpamMonitor");
   echo 
"<p class='red'>ERROR! Sorry, you may not spam this website!</p>";
   
//No form provided
 
include ("includes/footer.inc");
 die();
 }
//////////////////////////////////////////////////////////////////////////
// Validating name field, and calling function validate_firstname.
if (validate_firstname($firstname)) {
} else {    
    echo 
"<p class='red'>ERROR!
    Your firstname can include only alphabets and marks like: - and .<br />
Example: Ann or Ann-Christine or Ann.Christine</p>"
;
    
    include (
"includes/form.inc");
    include (
"includes/footer.inc");
  exit; 
}  
////////////////////////////////////////////////////////////////////////////
// Validating name field, and calling function validate_lastname.
if (validate_lastname($lastname)) {
} else {    
    echo 
"<p class='red'>ERROR!
    Your lastname can include only alphabets and marks like: -<br />
Example: Hill or Hill-Smith</p>"
;
    
    include (
"includes/form.inc");
    include (
"includes/footer.inc");
  exit; 
}  
//////////////////////////////////////////////////////////////////////////////
// Validating phone number
if (validate_phone($phone)) {
} else {    
    echo 
"<p class='red'>ERROR!
    Your phone number can include only numbers and marks like: (), - or space<br />
Example: (123) 123-1234</p>"
;

    include (
"includes/form.inc");
    include (
"includes/footer.inc");
  exit; 
}
/////////////////////////////////////////////////////////////////////////// 
// This if statement calls the function validate_email and checks to see if the 
// email address is in the correct format. If not, an error message is sent,
// from provided and an exit statement used to stop the page from processing further. 
 
if (validate_email($user_mail)) {
 echo 
$valid;
} else {
 echo 
"<p class='red'>ERROR! The entered email is invalid!
  Please enter a valid email address.</p>"
;
    include (
"includes/form.inc");
    include (
"includes/footer.inc");
  exit;
}

///////////////////////////////////////////////////////////////////////// 
    
mail("$my_email""$subject""$businessmailmsg""$headers");
    
mail("$user_mail""$subject""$usermailmsg""$headers");
     include (
"includes/thank.inc");
} else {
     include (
"includes/form.inc");
}
include (
"includes/footer.inc");
?>
<form action="<?php $_SREVER'PHP_SELF']?>" method="post" name="mailform" id="textbox">
<br /><h2 class="left">Practice Form</h2>
<fieldset>Firstname: *</fieldset><input name="firstname" type="text" value="<?php echo $firstname?>" /><br />
<fieldset>Lastname: *</fieldset><input name="lastname" type="text" value="<?php echo $lastname?>" /><br />
<fieldset>Phone (us): *</fieldset><input name="phone" type="text" value="<?php echo $phone?>" /><br />
<fieldset>E-mail: *</fieldset><input name="user_mail" type="text" value="<?php echo $user_mail?>" /><br />
<fieldset>Comments: *</fieldset><textarea name="comments" cols="55" rows="4"><?php echo $comments?></textarea>
<input name="submit" type="image" id="submitbutton" value=" Submit " src="images/submit.png" alt="Submit"/>
<br />
<p class="left">* = Required field</p>
</form>
<?php
// Define variables
/////////////////////////////////////////////////////////////////
$my_email "eikukaan.none@$dname";
$subject "Inroduction to PHP/week4 Form";
$message "You have recieved a comment.";
$headers "From: $my_email" "\r\n" "Return_Path: $my_email";
//////////////////////////////////////////////////////////////////
// email sent to business
$businessmailmsg =
 
"You have reseived an inquiry from:\n
Firstname: $firstname\n
Lastname: $lastname\n
Phone: $phone\n
Email: $user_mail\n
Comments:\n 
$comments"
;
//////////////////////////////////////////////////////////////////
//email sent to user
$usermailmsg =
"From: $websitename\n\n
Thank you for taking the time to visit our web site.\n
This is the information you submitted for your inquiry.\n
If this is not correct, please contact us at mailto:$my_email.\n\n
Firstame: $firstname\n
Lastname: $lastname\n
Phone: $phone\n
Email: $user_mail\n\n
Your comments:\n\n
$comments\n\n
Please feel free to write us with any comments or suggestions
so that we may better serve you.\n
mailto:$my_email\n\n"
;
/////////////////////////////////////////////////////////////////
$len_firstname 25;
$len_lastname 35;
$len_phone 15;

?>
<?php

function validate_firstname($value)
{    
    
$regexp "^[[:alpha:]]+((([ -][[:alpha:]]+){0,2})|( [[:alpha:]]\.))*$";
    
$valid 0;
   if (
eregi($regexp$value))
    {
        
$valid 1
} else {
        
$valid 0;
    }
    return  
$valid;
}
//////////////////////////////////////////////////////////////////////////////////////    
function validate_lastname($value)
{
    
$regexp "^([[:alpha:]]{2,5} ){0,2}[[:alpha:]]+([' -][[:alpha:]]+)*$";
    
$valid 0;
    if (
eregi($regexp$value))
    {
        
$valid 1;
} else {
        
$valid 0;
    }
    return  
$valid;
}
///////////////////////////////////////////////////////////////////////////////////////
function validate_phone($value)
{
    
$regexp "/^[\(]?(\d{3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/";
    
$valid 0;
    if (
preg_match($regexp$value))
    {
        
$valid 1;
} else {
        
$valid 0;
    }
    return  
$valid;
}
/////////////////////////////////////////////////////////////////////////////////

// Validating email
function validate_email($user_mail)
{
  
// Create the syntactical validation regular expression
  
$regexp "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
  
 
// Presume that the email is invalid
    
$valid 0;
    
 
//Validating the syntax
 
if (eregi($regexp$user_mail)) 
 {
 
   list(
$username,$domaintld) = split("@",$user_mail);
//////////////////////////////    
 // Validating the domain
 
if (getmxrr($domaintld,$mxrecords))
    
$valid 1;
} else {
    
$valid 0;
}
    return 
$valid;
}
//End of function
?>
 <div class="centered">
    <h2>Thank you for your inquiry.</h2>
    <p>You will resieve    confirmation email shortly and we will be in touch with you soon.
    <br />
    We appreciate your visiting our web site and sincerely hope, that we can be of service to you<br /> 
    now or in the future.<br />
    Feel free to write us with any comments or suggestions, that we may better serve you.<br />
    Also comments for this layout and code are welcome.<br /> 
  <a href="week4.php">Back to Form </a>
  </p>
  </div>