-4

我有一个简单的电子邮件表格,我根据自己的需要进行了编辑,一切正常......除了......

该表单的文本字段中已经存在单词,因此用户知道要输入什么。

这些话是

姓名 地址 邮编 电话 留言

如果用户尝试发送“空白电子邮件”......由于电子邮件检查和电话检查(电子邮件需要 @ + 。电话只需要数字和空格!)

我正在尝试做的是禁止使用“姓名”“地址”“邮政编码”和“电话”等词.....

留言对我来说无所谓!

这是我正在使用的脚本:

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "me@me.com";
$email_subject = "My Email";


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['address1']) ||
    !isset($_POST['postcode']) ||
    !isset($_POST['phone']) ||
    !isset($_POST['message'])) {
    died('We are sorry, but there appears to be a problem with the form you    submitted.');      
}

$name = $_POST['name']; // required
$email = $_POST['email']; // required
$address1 = $_POST['address1']; // required
$postcode = $_POST['postcode']; // required
$phone = $_POST['phone']; // required
$message = $_POST['message']; // not required

$error_message = "";

$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$address_exp = "/^[A-Za-z 0-9 .',-]+$/";
if(!preg_match($address_exp,$address1)) {
$error_message .= 'The Address you entered does not appear to be valid.<br />';
}
$postcode_exp = "/^[A-Za-z 0-9 .',-]+$/";
if(!preg_match($postcode_exp,$postcode)) {
$error_message .= 'The Postcode you entered does not appear to be valid.<br />';
}
$phone_exp = "/^[0-9 ]+$/";
if(!preg_match($phone_exp,$phone)) {
$error_message .= 'The Phone number you entered does not appear to be valid.<br />';
}

if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "name: ".clean_string($name)."\n";
$email_message .= "email: ".clean_string($email)."\n";
$email_message .= "address1: ".clean_string($address1)."\n";
$email_message .= "postcode: ".clean_string($postcode)."\n";
$email_message .= "phone: ".clean_string($phone)."\n";
$email_message .= "message: ".clean_string($message)."\n";

// create email headers
$headers = 'From: '.$whofrom=$_POST["email"];
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

希望你能帮助我,因为我不知道该怎么做!

4

1 回答 1

3

使用您正在测试的单词对所有单词进行数组检查。

例子:

$bad=array("Name", "Address", "Postcode", "Phone Message");

foreach ($_POST as $post) {
    if (in_array($post,$bad)) {
        died("Used a reserved word!");
    }
}
于 2013-05-27T17:01:25.443 回答