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     $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         <?php
38         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         <?php
54         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>

mail_process.php的代码:

 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 }

知识点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部分同样会当做条件与剧中{}中的一部分,只有条件为真的时候才会被客户端解析,否则会被忽略掉。

当然也可以直接用PHP判定语句生成错误信息,但是这样的话,就不方便对CSS信息使用CSS规则了(当然也可以使用,只是需要拐一道弯,不方便而已)。

例如,你可以直接使用:

if(bollen){

  echo "<span class="warning">Please enter your email address</span>";

}

时间: 2024-08-13 15:02:23

5.8 Adding headers and automating the reply address的相关文章

Odoo9.0模块开发全流程

构建Odoo模块 模块组成 业务对象 业务对象声明为Python类, 由Odoo自动载入. 数据文件 XML或CSV文件格式, 在其中声明了元数据(视图或工作流).配置数据(模块参数).演示数据等. Web控制器 处理Web浏览器发来的requests. 静态web数据 Web用到的图像, CSS或JavaScript文件. 模块结构 一个Odoo模块也是一个Python模块, 存放在一个目录中, 包含一个__init__.py文件, 用于导入其他Python模块. from . import

The Portable Executable File Format from Top to Bottom(每个结构体都非常清楚)

The Portable Executable File Format from Top to Bottom Randy KathMicrosoft Developer Network Technology Group Created: June 12, 1993 Click to open or copy the files in the EXEVIEW sample application for this technical article. Click to open or copy t

PrimeNG之FileUpload

--文件上传是一个支持拖放,多文件上传,自动上传,进度跟踪和验证的上传. Import import {FileUploadModule} from 'primeng/primeng'; Getting Started 文件上传需要一个URL属性为上传目标和一个名称来标识在后端的文件. <p-fileUpload name="myfile[]" url="http://localhost:3000/upload"></p-fileUpload>

Listview源码分析(1)

首先Listview继承关系: ListView  --extends-->  AbsListview  --extends-->  AdapterView  --extends-->  ViewGroup  --extends-->  View ListView的构造方法: 此时初始化listview的风格,间距 public ListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleR

RESTful API的设计原则

最近一直在做公司的一个API平台的项目,前后大约有半年多了,中间穿插了好多其他的项目一起做的.上周经理要求写文档,我就重新打开项目开始检阅之前的代码,发现好多地方当初设计的并不合理,忽然就想到,一个好的API平台,应该怎么来设计呢?有哪些规范要遵守呢?面对自己的项目,感觉好多地方都要改,但是已经有人在用了,怎么办?全都要改动吗?所以就上网找解决方案,然后就发现一精品贴,现转载过来,以备不时查阅. 原文地址:http://www.cnblogs.com/moonz-wu/p/4211626.htm

List加入headView后点击position错乱问题

1.问题描述 当ListView中加入了headerView或者footerView之后,调用ListView的OnItemClick监听事件之后,获取到的position不再是 我们期望的值,比如,当我点击的是第一行,结果它显示的是第二行的position. 2.问题分析 从ListView的源码中可以得到我们想要的答案: 1.addHeaderView(View v, Object data, boolean isSelectable): /** * Add a fixed view to

Erlang cowboy 处理不规范的client

Erlang cowboy 处理不规范的client Cowboy 1.0 參考 本章: Dealing with broken clients 存在很多HTTP协议的实现版本号. 很多广泛使用的client,如浏览器.十分符合规范.可是也有一些特殊的client很糟糕,不遵守规范. Cowboy尽可能地遵守规范,可是仍然无法处理所有可能的情形.Cowboy关注真是 web下的自然例子. 假设client不遵守HTTP规范可能会无法理解Cowboy的响应.有一些变通的方法,本章就说明这个问题.

好RESTful API的设计原则

说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间,如有人愿意转载请注明出处,谢谢^_^ Principles of good RESTful API Design 好RESTful API的设计原则 Good API design is hard! An API represents a contract between you and those who Consume your da

实习入职第二十天:从setRecyclerListener看listView回收机制

关于这个  setRecyclerListener函数在解决   listView滑出屏幕(包括向上滑出和向下滑出)处理相关UI操作或者释放相关资源,真的很好用, 比listView的setOnScrollListener事件的onScroll好用很多, 官网API解释这个方法是这样的: android.widget 接口 AbsListView.RecyclerListener 包容类: AbsListView public static interface AbsListView.Recyc