码迷,mamicode.com
首页 > 其他好文 > 详细

5.9 Building the message body

时间:2014-11-02 13:42:32      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   使用   

首先贴上到这一节所完成的代码:

form.php:

bubuko.com,布布扣
 1 <?php
 2 $errors = array();
 3 $missing = array();
 4 if (isset($_POST[‘send‘])) {
 5     $to = ‘david@example.com‘;
 6     $subject = ‘Feedback from contact form‘;
 7     $expected = array(‘name‘, ‘email‘, ‘comments‘);
 8     $required = array(‘name‘, ‘email‘, ‘comments‘);
 9     $headers = "From: webmaster@example.com\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 <pre>
77 <?php
78 if ($_POST && $mailSent) {
79     echo htmlentities($message, ENT_COMPAT, ‘utf-8‘);
80     echo ‘Headers: ‘ . htmlentities($headers, ENT_COMPAT, ‘utf-8‘);
81 }
82 ?>
83 </pre>
84 </body>
85 </html>
View Code

email_process.php

bubuko.com,布布扣
 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 }
39 
40 if (!$suspect && !$missing && !$errors) {
41     $message = ‘‘;
42     foreach ($expected as $item) {
43         if (isset($$item) && !empty($$item)) {
44             $val = $$item;
45         } else {
46             $val = ‘Not selected‘;
47         }
48         if (is_array($val)) {
49             $val = implode(‘, ‘, $val);
50         }
51         $item = str_replace(array(‘_‘, ‘-‘), ‘ ‘, $item);
52         $message .= ucfirst($item) . ": $val\r\n\r\n";
53     }
54     $message = wordwrap($message, 70);
55     
56     $mailSent = true;
57 }
View Code

 

知识点1:PHP中如何声明变量?

很简单的知识,只是我刚好忘记了,这个时候再复习一下吧。

主要资料:PHP.NET/MANUAL:http://php.net/manual/zh/language.variables.basics.php(请仔细阅读,虽然简单,但也有很多小的知识)。

关于变量的初始化,有两个例子:

例子1:

1 <?php
2 $test;
3 if($test){
4     echo ‘$test is set‘;
5 }else{
6     echo ‘$test is not set yet‘;
7 };
8 ?>

以上代码输出:


Notice: Undefined variable: test in H:\xampp\htdocs\test\test.php on line 3

$test is not set yet

例子2:

1 <?php
2 $test;
3 if(isset($test)){
4     echo ‘$test is set‘;
5 }else{
6     echo ‘$test is not set yet‘;
7 };
8 ?>

以上代码输出(E文不好,也许set用在此处不对,但是能明白意思就行~~):

$test is not set yet

知识点2:foreach语句;

从这一章的例子来看,这个语句非常常用,首先看看几个比较权威的参考文档:

首先,关于数组的知识,请看另一篇随笔:http://www.cnblogs.com/huaziking/p/4067487.html

PHP.net/manual:http://cn2.php.net/manual/zh/control-structures.foreach.php

我以前对遍历的概念有误解,现在说一下自己的基本理解:foreach不是创造一个新数组,也不是将数组分割成一对一对的值,而是这个数组本来就存在,foreach是告诉机器:请把这个数组给我找出来,然后按照{}里面的语句进行一些操作。

foreach的内容比较多,可以看看另一篇单独介绍它的文章:http://www.cnblogs.com/huaziking/p/4068340.html

知识点3:in_array

参考资料:PHP手册:http://cn2.php.net/manual/zh/function.in-array.php

in_array是个很简单的函数,意思是:在数组中搜索给定的值

基本形式是:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

首先,整个语句的返回值是boolean,如果找到了就是true,如果没有找到就是false。

parameter1:$needle  待搜索的值,可以是任意类型的值。

1> 如果$needle是字符串的话,匹配是区分大小写的。(思考:如果我不想区分大小写,就想用模糊匹配该怎么去做呢?

1 <?php
2 $os = array("Mac", "NT", "Irix", "Linux");
3 if (in_array("Irix", $os)) {
4     echo "Got Irix";
5 }
6 if (in_array("mac", $os)) {
7     echo "Got mac";
8 }
9 ?>

因为in_array是区分大小写的,in_array("mac",$os) == false; 所以以上代码会输出:

Got Irix

2>在 PHP 版本 4.2.0 之前,needle 不允许是一个数组(但是之后的版本是允许其是一个数组的)。

 1 <?php
 2 $a = array(array(‘p‘, ‘h‘), array(‘p‘, ‘r‘), ‘o‘);
 3 
 4 if (in_array(array(‘p‘, ‘h‘), $a)) {
 5     echo "‘ph‘ was found\n";
 6 }
 7 
 8 if (in_array(array(‘f‘, ‘i‘), $a)) {
 9     echo "‘fi‘ was found\n";
10 }
11 
12 if (in_array(‘o‘, $a)) {
13     echo "‘o‘ was found\n";
14 }
15 ?>

以上代码会输出:

‘ph‘ was found ‘o‘ was found

parameter2:$haystack:定义我们要在哪个数组中进行搜索。

parameter3:$strict,boolean值,定义是否严格匹配我们要搜索的数据类型和数组中的数据类型。默认值是false。如果设置为true则 ‘1‘和1是不相等的,因为两个的数据类型是不一样的。

 1 <?php
 2 $a = array(‘1.10‘, 12.4, 1.13);
 3 
 4 if (in_array(‘12.4‘, $a, true)) {
 5     echo "‘12.4‘ found with strict check\n";
 6 }
 7 
 8 if (in_array(1.13, $a, true)) {
 9     echo "1.13 found with strict check\n";
10 }
11 ?>

以上代码会输出:(说明这个时候数据类型也是要求完全匹配的。)

1.13 found with strict check

知识点4:$$key(请回头看之前的课程);

具体到某一个实例,在数组中,key对应value,如果$key == ‘name‘;,则$$key == $name,但是$name依然是一个key,而不是一个value,它有一个对应的value,可以是David,或者Kobe.

知识点5:inplode

implode() 函数和explode()函数都是很简单的函数,参考阅读:

1、http://www.w3cschool.cn/func_string_implode.html

2、http://www.w3school.com.cn/php/func_string_explode.asp

知识点6:str_replace;

这也是一个简单的函数,参考阅读:

1、http://cn2.php.net/str_replace

知识点7:\r\n\r\n

\r\n 一般一起用,用来表示键盘上的回车键,也可只用 \n。

知识点8:wordwrap

打断字符串为指定数量的字串。

http://php.net/manual/zh/function.wordwrap.php

知识点9:htmlentitles;

 htmlentities() 函数把字符转换为 HTML 实体。

语法结构:

htmlentities(string,quotestyle,character-set) 

 

参数描述
string 必需。规定要转换的字符串。
quotestyle

可选。规定如何编码单引号和双引号。

  • ENT_COMPAT - 默认。仅编码双引号。
  • ENT_QUOTES - 编码双引号和单引号。
  • ENT_NOQUOTES - 不编码任何引号。
character-set

可选。字符串值,规定要使用的字符集。

  • ISO-8859-1 - 默认。西欧。
  • ISO-8859-15 - 西欧(增加 Euro 符号以及法语、芬兰语字母)。
  • UTF-8 - ASCII 兼容多字节 8 比特 Unicode
  • cp866 - DOS 专用 Cyrillic 字符集
  • cp1251 - Windows 专用 Cyrillic 字符集
  • cp1252 - Windows 专用西欧字符集
  • KOI8-R - 俄语
  • GB2312 - 简体中文,国家标准字符集
  • BIG5 - 繁体中文
  • BIG5-HKSCS - Big5 香港扩展
  • Shift_JIS - 日语
  • EUC-JP - 日语

 

 

《完》

 

5.9 Building the message body

标签:des   style   blog   http   io   color   ar   os   使用   

原文地址:http://www.cnblogs.com/huaziking/p/4067412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!