Hello! 🙂 I'm new to PHP. I'm trying to understand why my PHP file sends me to error page even though I entered my email address. I wonder how to fix this and make PHP file work?
My PHP file is a mixture of copy & pastes from this page, this other page along with my newbie PHP attempts.
Here's the XHTML form code:
<form method="post" action="sendmail.php" class="contactform">
<ol>
<li>
<label for="name">Name<em class="required">*</em></label>
<input type="text" name="name" id="name" />
</li>
<li>
<label for="address1">Address 1<em class="required">*</em></label>
<input type="text" name="address1" id="address1" />
</li>
<li>
<label for="address2">Address 2</label>
<input type="text" name="address2" id="address2" />
</li>
<li>
<label for="city">City<em class="required">*</em></label>
<input type="text" name="city" id="city" />
</li>
<li>
<label for="state">State<em class="required">*</em></label>
<input type="text" name="state" id="state" />
</li>
<li>
<label for="zipcode">Zip Code<em class="required">*</em></label>
<input type="text" name="zipcode" id="zipcode" />
</li>
<li>
<label for="phone">Phone Number<em class="required">*</em></label>
<input type="text" name="phone" id="phone" />
</li>
<li>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" />
</li>
<li>Please enter the code below into the text box.<br />
<img src="CaptchaSecurityImages.php" alt="code for security purpose" /><em class="required">*</em>
<input type="text" name="security_code" id="security_code" />
</li>
<li>
<label for="message">Message<em class="required">*</em></label><br />
<textarea name="message" id="message" rows="15" cols="50"></textarea><br />
<input type="submit" name="submit" value="Submit" id="submit" />
<input type="reset" name="reset" value="Reset" id="reset" />
</li>
</ol>
</form>
here's the PHP file in its entirety:
<?php
session_start();
if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
// Insert your code for processing the form here, e.g emailing the submission, entering it into a database.
$name = $_GET['name'];
$address1 = $_GET['address1'];
$address2 = $_GET['address2'];
$city = $_GET['city'];
$state = $_GET['state'];
$zipcode = $_GET['zipcode'];
$phone = $_GET['phone'];
$email = $_GET['email'];
$message = $_GET['message'];
function is_valid_email($email) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}
function contains_bad_str($str_to_test) {
$bad_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"Content-Transfer-Encoding:"
,"bcc:"
,"cc:"
,"to:"
);
foreach($bad_strings as $bad_string) {
if(eregi($bad_string, strtolower($str_to_test))) {
echo "mail not being sent.";
exit;
}
}
}
function contains_newlines($str_to_test) {
if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
echo "mail not being sent.";
exit;
}
}
if($_SERVER['REQUEST_METHOD'] != "POST"){
echo("Unauthorized attempt to access page.");
exit;
}
if (!is_valid_email($email)) {
header( "Cache-Control: no-cache" );
header( "Location: http://www.wordandworshipchurch.org/error.html" );
exit;
}
contains_bad_str($name);
contains_bad_str($address1);
contains_bad_str($address2);
contains_bad_str($city);
contains_bad_str($state);
contains_bad_str($zipcode);
contains_bad_str($phone);
contains_bad_str($email);
contains_bad_str($message);
contains_newlines($name);
contains_newlines($address1);
contains_newlines($address2);
contains_newlines($city);
contains_newlines($state);
contains_newlines($zipcode);
contains_newlines($phone);
contains_newlines($email);
contains_newlines($message);
if (!isset($_GET['name'],$_GET['address1'],$_GET['address2'],$_GET['city'],$_GET['state'],$_GET['zipcode'],$_GET['phone'],$_GET['email'],$_GET['message'])) {
header( "Cache-Control: no-cache" );
header( "Location: http://www.wordandworshipchurch.org/contactus.html" );
}
elseif (empty($name) || empty($address1) || empty($city) || empty($state) || empty($zipcode) || empty($phone) || empty($email) || empty($message)) {
header( "Cache-Control: no-cache" );
header( "Location: http://www.wordandworshipchurch.org/error.html" );
}
else {
mail( "(my email address here)", "Contact Form Results",
" Name: $name \n Address 1: $address1 \n Address 2: $address2 \n City: $city \n State: $state \n Zip Code: $zipcode \n Phone: $phone \n Email: $email \n Message: $message");
header( "Cache-Control: no-cache" );
header( "Location: http://www.wordandworshipchurch.org/thankyou.html" );
}
unset($_SESSION['security_code']);
} else {
// Insert your code for showing an error message here
header( "Cache-Control: no-cache" );
header( "Location: http://www.wordandworshipchurch.org/error.html" );
}
?>
Obviously, I'm new to adding security to online form. Plenty of room for learning PHP & improvement 🙂 Suggestions & advices will be appreciated 🙂