1. 云栖社区>
  2. PHP教程>
  3. 正文

5.8 Adding headers and automating the reply address

作者:用户 来源:互联网 时间:2017-12-01 18:52:18

5.8 Adding headers and automating the reply address - 摘要: 本文讲的是5.8 Adding headers and automating the reply address, 首先贴上到目前为止完成的代码: form.php的代码: 1 <?php 2 $errors = array(); 3 $missing = array(); 4 if (isset($_POST['send'])) { 5 $t

首先贴上到目前为止完成的代码:

form.php的代码:

5.8 Adding headers and automating the reply address
 1 <?php 2 $errors = array(); 3 $missing = array(); 4 if (isset($_POST['send'])) { 5 $to = '[email protected]'; 6 $subject = 'Feedback from contact form'; 7 $expected = array('name', 'email', 'comments'); 8 $required = array('name', 'email', 'comments'); 9 $headers = "From: [email protected]/r/n";10 $headers .= "Content-type: text/plain; charset=utf-8";11 require './includes/mail_process.php';12 }13 ?>14 <!DOCTYPE HTML>15 <html>16 <head>17 <meta charset="utf-8">18 <title>Contact Us</title>19 <link href="../styles.css" rel="stylesheet" type="text/css">20 </head>21 22 <body>23 <h1>Contact Us</h1>24 <?php if ($_POST && $suspect) { ?>25 <p class="warning">Sorry your mail could not be be sent.</p>26 <?php } elseif ($errors || $missing) { ?>27 <p class="warning">Please fix the item(s) indicated.</p>28 <?php }?>29 <form name="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">30 <p>31 <label for="name">Name:32 <?php if ($missing && in_array('name', $missing)) { ?>33 <span class="warning">Please enter your name</span>34 <?php } ?>35 </label>36 <input type="text" name="name" id="name"37 <?php38 if ($errors || $missing) {39 echo 'value="' . htmlentities($name, ENT_COMPAT, 'utf-8') . '"';40 }41 ?>42 >43 </p>44 <p>45 <label for="email">Email:46 <?php if ($missing && in_array('email', $missing)) { ?> 47 <span class="warning">Please enter your email address</span>48 <?php } elseif (isset($errors['email'])) { ?>49 <span class="warning">Invalid email address</span>50 <?php } ?>51 </label>52 <input type="text" name="email" id="email"53 <?php54 if ($errors || $missing) {55 echo 'value="' . htmlentities($email, ENT_COMPAT, 'utf-8') . '"';56 }57 ?>58 >59 </p>60 <p>61 <label for="comments">Comments:62 <?php if ($missing && in_array('comments', $missing)) { ?>63 <span class="warning">You forgot to add your comments</span>64 <?php } ?>65 </label>66 <textarea name="comments" id="comments"><?php 67 if ($errors || $missing) {68 echo htmlentities($comments, ENT_COMPAT, 'utf-8');69 }70 ?></textarea>71 </p>72 <p>73 <input type="submit" name="send" id="send" value="Send Comments">74 </p>75 </form>76 </body>77 </html>
View Code

mail_process.php的代码:

5.8 Adding headers and automating the reply address
 1 <?php 2 $suspect = false; 3 $pattern = '/Content-Type:|Bcc:|Cc:/i'; 4  5 function isSuspect($val, $pattern, &$suspect) { 6 if (is_array($val)) { 7 foreach ($val as $item) { 8 isSuspect($item, $pattern, $suspect); 9 }10 } else {11 if (preg_match($pattern, $val)) {12 $suspect = true;13 }14 }15 }16 17 isSuspect($_POST, $pattern, $suspect);18 19 if (!$suspect) {20 foreach ($_POST as $key => $value) {21 $temp = is_array($value) ? $value : trim($value);22 if (empty($temp) && in_array($key, $required)) {23 $missing[] = $key;24 $$key = '';25 } elseif(in_array($key, $expected)) {26 $$key = $temp;27 }28 }29 }30 31 if (!$suspect && !empty($email)) {32 $validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);33 if ($validemail) {34 $headers .= "/r/nReply-to: $validemail";35 } else {36 $errors['email'] = true;37 }38 }
View Code

知识点1:filter_input

函数filter_input的用法: http://www.w3school.com.cn/php/func_filter_input.asp http://php.net/manual/en/function.filter-input.php

此处实例:mail_process.php的行32:

1 $validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

INPUT_POST是数据来源,同样的,还可以有其他来源,比如:One of INPUT_GET , INPUT_POST , INPUT_COOKIE , INPUT_SERVER , or INPUT_ENV( 数据来源于PHP .net/ manual .

'email'是variable_name,就是你要过滤的variable。

FILTER_VALIDATE_EMAIL 是规律的规则,这个是和发邮件地址的规则,同样的还有许多其他的规则(无需记忆,使用时候知道哪里去找就行了),扩展阅读: http://php.net/manual/en/filter.filters.php

知识点2:email header的一些写法格式:

首先看form.php的行9,10代码:

9$headers = "From: [email protected]/r/n";10 $headers .= "Content-type: text/plain; charset=utf-8";

我觉得这个没什么好说的,就是一个格式标准,按照这个格式来写机器才能读得懂。

知识点3:$errors['email']=true;

具体请看mail_process.php 行36的代码:

36$errors['email'] = true;

这里应该是将email放到了$errors这个数组中了( 这个有待验证,但至少$errors['email'] == true;这就够我们后面用的了 )。

知识点4:How to display error message?

这个只是点在前面的笔记中有记录,但是前面的笔记被不小心删除了,这个再补充一下。

请看form.php 行45-50;

45<label for="email">Email:46 <?php if ($missing && in_array('email', $missing)) { ?> 47<span class="warning">Please enter your email address</span>48<?php } elseif (isset($errors['email'])) { ?>49<span class="warning">Invalid email address</span>50<?php } ?>

php的if条件语句可以可以头尾分开放在两个<?php ?>中,中间的HTML部分同样会当做条件与剧中{}中的一部分,只有条件为真的时候才会被客户端解析,否则会被忽略掉。

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。