# 過濾與清理(Filtering and Sanitizing)
清理用戶輸入是軟件開發中很重要的一個環節。信任或者忽略對用戶輸入數據作清理可能會導致 對應用內容(主要是用戶數據),甚至你應用所處在的服務器的非法訪問。

[Full image (from xkcd)](http://xkcd.com/327/)
此[Phalcon\\Filter](http://docs.iphalcon.cn/api/Phalcon_Filter.html)組件提供了一系列通用可用的過濾器和數據清理助手。它提供了圍繞于PHP過濾擴展的面向對象包裝。
## 內置過濾器類型(Types of Built-in Filters)
以下是該容器提供的內置過濾器:
| 名稱 | 描述 |
| --- | --- |
| string | 去除標簽和HTML實體,包括單雙引號 |
| email | 刪掉除字母、數字和 !#$%&\*+-/=?^\_`{|}~@.\[\] 外的全部字符 |
| int | 刪掉除R數字、加號、減號外的全部字符 |
| float | 刪掉除數字、點號和加號、減號外的全部字符 |
| alphanum | 刪掉除\[a-zA-Z0-9\]外的全部字符 |
| striptags | 調用[strip\_tags](http://www.php.net/manual/en/function.strip-tags.php)方法 |
| trim | 調用[trim](http://www.php.net/manual/en/function.trim.php)方法 |
| lower | 調用[strtolower](http://www.php.net/manual/en/function.strtolower.php)方法 |
| upper | 調用[strtoupper](http://www.php.net/manual/en/function.strtoupper.php)方法 |
## 清理數據(Sanitizing data)
清理是指從一個值中移除特定字符的過程,此過程對用戶和應用不是必須,也不是他們想得到的。 通過清理輸入,我們確保了應用的完整性和正確性。
~~~
<?php
use Phalcon\Filter;
$filter = new Filter();
// 返回 "someone@example.com"
$filter->sanitize("some(one)@exa\mple.com", "email");
// 返回 "hello"
$filter->sanitize("hello<<", "string");
// 返回 "100019"
$filter->sanitize("!100a019", "int");
// 返回 "100019.01"
$filter->sanitize("!100a019.01a", "float");
~~~
## 在控制器中使用清理(Sanitizing from Controllers)
當接收到GET或POST的數據時(通過請求對象),你可以在控制器中訪問一個[Phalcon\\Filter](http://docs.iphalcon.cn/api/Phalcon_Filter.html)對象。 第一個參數是等待獲得變量的名字,第二個參數是將應用在此變量的過濾器。
~~~
<?php
use Phalcon\Mvc\Controller;
class ProductsController extends Controller
{
public function indexAction()
{
}
public function saveAction()
{
// 從輸入中清理price
$price = $this->request->getPost("price", "double");
// 從輸入中清理email
$email = $this->request->getPost("customerEmail", "email");
}
}
~~~
## 過濾動作參數(Filtering Action Parameters)
接下來的示例演示了在一個控制器的動作中如何清理動作的參數:
~~~
<?php
use Phalcon\Mvc\Controller;
class ProductsController extends Controller
{
public function indexAction()
{
}
public function showAction($productId)
{
$productId = $this->filter->sanitize($productId, "int");
}
}
~~~
## 過濾數據(Filtering data)
此外,[Phalcon\\Filter](http://docs.iphalcon.cn/api/Phalcon_Filter.html)也提供了可以進行刪除或者修改輸入數據以滿足我們需要的格式的過濾器。
~~~
<?php
use Phalcon\Filter;
$filter = new Filter();
// 返回 "Hello"
$filter->sanitize("<h1>Hello</h1>", "striptags");
// 返回 "Hello"
$filter->sanitize(" Hello ", "trim");
~~~
## Combining Filters
You can also run multiple filters on a string at the same time by passing an array of filter identifiers as the second parameter:
~~~
<?php
use Phalcon\Filter;
$filter = new Filter();
// 返回 "Hello"
$filter->sanitize(
" <h1> Hello </h1> ",
[
"striptags",
"trim",
]
);
~~~
## 創建過濾器(Creating your own Filters)
你可以將你自己的過濾器添加到[Phalcon\\Filter](http://docs.iphalcon.cn/api/Phalcon_Filter.html)。過濾器的方法可以是匿名函數:
~~~
<?php
use Phalcon\Filter;
$filter = new Filter();
// 使用匿名函數
$filter->add(
"md5",
function ($value) {
return preg_replace("/[^0-9a-f]/", "", $value);
}
);
// 利用md5過濾器清理
$filtered = $filter->sanitize($possibleMd5, "md5");
~~~
或者,如果你愿意,你可以在類中實現過濾器:
~~~
<?php
use Phalcon\Filter;
class IPv4Filter
{
public function filter($value)
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
}
$filter = new Filter();
// 使用對象
$filter->add(
"ipv4",
new IPv4Filter()
);
// 利用"ipv4"過濾器清理
$filteredIp = $filter->sanitize("127.0.0.1", "ipv4");
~~~
## 復雜的過濾與清理(Complex Sanitizing and Filtering)
你可以使用PHP本身提供的優秀過濾器擴展。請查看對應的文檔:[PHP文檔上的數據過濾器](http://www.php.net/manual/en/book.filter.php)
## 自定義過濾器(Implementing your own Filter)
如需創建你自己的過濾器并代替Phalcon提供的過濾器,你需要實現[Phalcon\\FilterInterface](http://docs.iphalcon.cn/api/Phalcon_FilterInterface.html)接口。
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl