-1

trying to setup the webpage with this PHP. I first got the blank page, then had it display the error(s) which was an internal server error. After a bit of research I saw that it's mostly the code that causes issues. If anyone would be kind enough to look this through for any possible solutions, I'd appreciate it. There is some more html code before this although it does not include any of the PHP.

    <?php
$subjectPrefix = 'Site Contact Request';
$emailTo = '***@****.com';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name     = stripslashes(trim($_POST['form-name']));
    $email    = stripslashes(trim($_POST['form-email']));
    $tel      = stripslashes(trim($_POST['form-tel']));
    $assunto  = stripslashes(trim($_POST['form-assunto']));
    $mensagem = stripslashes(trim($_POST['form-mensagem']));
    $pattern  = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
    if (preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $assunto)) {
        die("Header injection detected");
    }
    $emailIsValid = preg_match('/^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/', $email);
    if($name && $email && $emailIsValid && $assunto && $mensagem){
        $subject = "$subjectPrefix";
        $body = "Name: $name <br /> Subject: $assunto <br /> Email Address: $email <br /> Telephone Number: $tel <br /> Message: $mensagem";
        $headers  = 'MIME-Version: 1.1' . PHP_EOL;
        $headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
        $headers .= "From: $name <$email>" . PHP_EOL;
        $headers .= "Return-Path: $emailTo" . PHP_EOL;
        $headers .= "Reply-To: $email" . PHP_EOL;
        $headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    } else {
        $hasError = true;
    }
}
?>

    <?php if(isset($emailSent) && $emailSent): ?>
        <div class="col-md-6 col-md-offset-3">
            <div class="alert alert-success text-center">Your message has been sent.</div>
        </div>
    <?php else: ?>
        <?php if(isset($hasError) && $hasError): ?>
        <div class="col-md-5 col-md-offset-4">
            <div class="alert alert-danger text-center">An error occurred. Please try later.</div>
        </div>
        <?php endif; ?>

    <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" id="contact-form" class="comment_form" role="form" method="post">
        <div class="row-fluid">
            <div class="span6">
                <input type="text" class="form-control" id="form-name" name="form-name" placeholder="Name" required>
            </div>
            <div class="span6">
                <input type="email" class="form-control" id="form-email" name="form-email" placeholder="Email" required>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span6">
                <input type="tel" class="form-control" id="form-tel" name="form-tel" placeholder="Telephone (Optional)">
            </div>
            <div class="span6">
                <input type="text" class="form-control" id="form-assunto" name="form-assunto" placeholder="Subject" required>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span8">
                <textarea id="form-mensagem" name="form-mensagem" placeholder="Message"></textarea>
            </div>
            <div class="span4">
                <button class="btn" type="submit"><i class="li_paperplane"></i>Send message</button>
            </div>
        </div>
    </form>
4
  • If you already tried error_reporting(-1); and ini_set('display_errors', 'On'); and got an "Internal Server Error", and not a PHP error, then you probably have either a permissions or ownership issue with your php file....
    – David R.
    May 13, 2015 at 4:46
  • You are missing the endif; from <?php if(isset($emailSent) && $emailSent): ?>/<?php else: ?>
    – Sean
    May 13, 2015 at 4:47
  • @Sean you're right, good catch. 2 if's and only one endif.
    – David R.
    May 13, 2015 at 4:50
  • Please up-vote the answer also Feb 3, 2016 at 18:35

1 Answer 1

0

You are not closing one if condition at starting, please modify it like this:-

<?php if(isset($emailSent) && $emailSent): ?>
        <div class="col-md-6 col-md-offset-3">
            <div class="alert alert-success text-center">Your message has been sent.</div>
        </div>
    <?php else: ?>
        <?php if(isset($hasError) && $hasError): ?>
        <div class="col-md-5 col-md-offset-4">
            <div class="alert alert-danger text-center">An error occurred. Please try later.</div>
        </div>
        <?php endif; ?>
        <?php endif; ?> // this is missing and based on your functionality you can move it to anywhere else

Output:- http://prntscr.com/74n8d3

Note:- this is very first html started with if condition. depend on your functionality you need to adjust it whereever you want.