首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 輸入數據統一處理類:封裝 GET/POST/COOKIE/FILE 的過濾與類型轉換

     編輯:PHP 相關知識

設計目標

在 Web 開發中,對用戶輸入進行統一的獲取、過濾、類型轉換是安全與穩健的基礎。本文提供一個單例模式的 cls_request 類,封裝了對 $_GET、$_POST、$_REQUEST、$_COOKIE、$_FILES 的標準化處理。

核心功能

  • 單例模式:全局唯一實例,避免重複初始化。
  • 數據預處理:構造函數中自動 trim 字符串,保留數組結構(如 checkbox)。
  • 類型安全獲取:提供 get_num/post_num 等方法自動轉換為整型,非數字返回 false。
  • 字符串過濾:提供 get_string/post_string 等方法,默認調用 htmlspecialchars(ENT_QUOTES) 防 XSS,可選關閉。
  • 文件上傳透傳:filedata() 直接返回 $_FILES 子項。

完整類代碼

<?php
class cls_request {
    private $getdata, $postdata, $requestdata, $filedata, $cookiedata;
    static private $_instance;

    private function __construct() {
        $this->getdata      = self::format_data($_GET);
        $this->postdata     = self::format_data($_POST);
        $this->requestdata  = array_merge($this->getdata, $this->postdata);
        $this->cookiedata   = self::format_data($_COOKIE);
        $this->filedata     = self::format_data($_FILES);
    }

    public static function get_instance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    // --- 數值型獲取 ---
    public function get_num($key)      { return $this->_get_typed($this->getdata, $key); }
    public function post_num($key)     { return $this->_get_typed($this->postdata, $key); }
    public function request_num($key)  { return $this->_get_typed($this->requestdata, $key); }
    public function cookie_num($key)   { return $this->_get_typed($this->cookiedata, $key); }

    private function _get_typed($source, $key) {
        if (!isset($source[$key])) return false;
        return $this->to_num($source[$key]);
    }

    // --- 字符串獲取 ---
    public function get_string($key, $filter = true)  { return $this->_get_str($this->getdata, $key, $filter); }
    public function post_string($key, $filter = true) { return $this->_get_str($this->postdata, $key, $filter); }
    public function request_string($key, $filter = true) { return $this->_get_str($this->requestdata, $key, $filter); }
    public function cookie_string($key, $filter = true) { return $this->_get_str($this->cookiedata, $key, $filter); }

    private function _get_str($source, $key, $filter) {
        if (!isset($source[$key])) return false;
        return $filter ? $this->filter_string($source[$key]) : $source[$key];
    }

    public function filedata($key) { return isset($this->filedata[$key]) ? $this->filedata[$key] : null; }

    // --- 內部工具方法 ---
    private static function format_data($data) {
        $result = array();
        if (!is_array($data)) return $result;
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $result[$key] = $value; // 保留數組(如 checkbox[])
            } else {
                $result[$key] = trim($value);
            }
        }
        return $result;
    }

    private function to_num($num) {
        return is_numeric($num) ? intval($num) : false;
    }

    private function filter_string($data) {
        if ($data === null) return false;
        if (is_array($data)) {
            foreach ($data as $k => $v) $data[$k] = htmlspecialchars($v, ENT_QUOTES);
            return $data;
        }
        return htmlspecialchars($data, ENT_QUOTES);
    }
}
?>

使用示例

<?php
require 'cls_request.php';
$req = cls_request::get_instance();

// 獲取整型 ID,自動過濾非數字
$id = $req->get_num('id'); 
if ($id === false) exit('非法 ID');

// 獲取過濾後的字符串(防 XSS)
$username = $req->post_string('username');

// 獲取原始字符串(不轉義,如富文本編輯器內容)
$content = $req->post_string('content', false);

// 文件上傳信息
$fileInfo = $req->filedata('avatar');
?>

注意事項

  • 此類針對 PHP 5.x 設計,現代框架(Laravel, Symfony, Yii2 等)均內置更強大的 Request 對象與驗證組件,新項目建議直接使用框架原生功能。
  • htmlspecialchars 僅防輸出型 XSS,數據入庫前仍需使用預處理語句防注入。
  • $_REQUEST 合併順序受 request_order 配置影響,建議顯式使用 GET/POST 方法以避免歧義。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved