Parse error: syntax error, unexpected end of file in your code

I keep getting this error: ‘Parse error: syntax error, unexpected end of file in your code’. The error is linked to the last line of the code.

<?php
$result = $name = $email = $phone = $message = $human = "";
$errName = $errEmail = $errPhone = $errMessage = $errHuman = "";
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
         //valid address on your web server
        $from = '[email protected] ';
        //your email address where you wish to receive mail
        $to = '[email protected]';
        $subject = 'Contact from CCCC';
        $headers = "From:$from\r\nReply-to:$email";
        $body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message: $message";
if (empty($_POST["name"])) {
$errName = "Please enter your name.";
} else {
    $name = test_input($_POST["name"]);
if (empty($_POST["email"])) {
$errEmail = "Please enter your email address.";
} else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $errEmail = "Invalid email format.";
    }
}
if (empty($_POST["message"])) {
$errMessage = "Please enter your message.";
} else {
    $message = test_input($_POST["message"]);
}
if (empty($_POST["human"])) {
$errHuman = "Please enter the sum.";
} else {
     if ($human !== 12) {
     $errHuman = 'Wrong answer. Please try again.';
        }
}
if (!$errName && !$errEmail && !$errPhone &&  !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success"><h2><span class="glyphicon glyphicon-ok"></span> Message sent!</h2><h3>Thank you for contacting us. Someone will be in touch with you soon.</h3></div>';
    } else {
        $result='<div class="alert alert-danger"><h2><span class="glyphicon glyphicon-warning-sign"></span> Sorry there was a form processing error.</h2> <h3>Please try again later.</h3></div>';
       }
    }
}
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   $data = (filter_var($data, FILTER_SANITIZE_STRING));
   return $data;
}
?>

<!DOCTYPE html>
<html lang="en">
   <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="icon" type="image/x-icon" href="img/favicon.ico">
   
    <title>Contact Us</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link id="skin" href="default.css" rel="stylesheet">
	
    <!--[if lt IE 9]>
      <script src="js/html5shiv.min.js"></script>
      <script src="js/respond.min.js"></script>
    <![endif]-->
      
    <!--[if IE 9]>  
      <link href="css/ie.css" rel="stylesheet">
    <![endif]-->
    
   <!-- Put Google Analytics in here -->  
   <style>
	.center-block {float:none}
	.human {margin: 0 0 0 12px}
	</style>
  </head>

	<header>
		<a href="index1.html" class="logo"><img src="img/logo.png" alt="" /></a>
	</header>

<body>	
	<!-- Start contain wrapp -->
	<div class="contain-wrapp">	
		<div class="container">
				<div class="col-md-12">
					<div class="section-heading">
						<h4>WE ARE HERE TO HELP</h4>
						<p>Contact Us</p>
					</div>
				</div>		
				<div class="row">
					<div class="col-md-12">
						<div class="col-md-12 center-block">
						<h3>You can contact us using this form</h3>
						<!--begin HTML Form-->
						<form class="form-horizontal" role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
						 <!--when submit button is clicked, show results here-->
						<div class="form-group">
						<div class="col-sm-10 col-sm-offset-2">
						<?php echo $result;?>
						</div>
						</div>
						<div class="form-group">
						<label for="name" class="col-sm-3 control-label">Name:</label>
						<div class="col-sm-9">
						<input type="text" class="form-control" id="name" name="name" placeholder="First & Last"value="<?php echo $name;?>">
						<span class="required small"><?php echo $errName;?></span>
						</div>
						</div>
						<div class="form-group">
						<label for="email" class="col-sm-3 control-label">Email: </label>
						<div class="col-sm-9">
						<input type="email" class="form-control" id="email" name="email" placeholder="[email protected]" value="<?php echo $email;?>">
						<span class="required small"><?php echo $errEmail;?></span>
						</div>
						</div>
						<div class="form-group">
						<label for="message" class="col-sm-3 control-label">Message:</label>
						<div class="col-sm-9">
						<textarea class="form-control" row="4" name="message" placeholder="Tell us your story"></textarea><?php echo $message;?></textarea>
						<span class="required small"><?php echo $errMessage;?></span>
						</div>
						</div>
						<div class="form-group">
						<label for="human" class="col-sm-3 control-label">Human Test:</label>
						<div class="col-sm-4">
						<h3 class="human">6 + 6 = ?</h3>
						<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
						</div>
						</div>
						<div class="form-group">
						<div class="col-sm-offset-3 col-sm-6 col-sm-offset-3">
						<button type="submit" id="submit" name="submit" class="btn-lg btn-primary btn-block">SUBMIT</button>
						</div>
						</div>
						<!--end Form--></form>
						</div>
					</div>
				</div>
		</div>
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="js/main.js"></script>
 </body>
</html>

Two things that I observe

  1. I recommend that you use a 3rd Party Mailer (I personally use PHPMailer) that way all the dirty work (security and configuration) is done by PHPMailer or another 3rd Party Mailer.

  2. Since you’re using a 3rd Party Mailer you don’t need the following:

[php] function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = (filter_var($data, FILTER_SANITIZE_STRING));
return $data;
}[/php]

Someone on a different forum once told me that if a person isn’t a PHP Security expert or does that for a living one should not try to write their own security routines. In addition to that I look at it why reinvent the wheel when it is already done for your, besides it frees up you time doing mundane coding.

P.S. I would also would get rid of that self-captcha routine for bots will easily bypass that. There is a proven captcha called hidden captcha (I believe that is what it is called) just Google it and I’m pretty sure you find examples on how it works. It’s not 100 percent effective and I would not use it on any big project, but for a small project I find it does pretty well.

If you format the php, you’ll see an opening bracket that is missing a matching closing bracket. This is what proper indentations are so important, it helps you catch things like that faster.

Sponsor our Newsletter | Privacy Policy | Terms of Service