Phalcon自定義過濾器及覆蓋原有過濾器

##2016-11-16日更新:在Phalcon 3.0.1中測試發現get方法會過濾單引號和雙引號了php


發現phalcon自帶的string過濾器$request->get('key', 'string') 不會過濾單引號和雙引號,這個很危險,因此在前臺模塊''fronted''的''di''注入的時候寫一個''string''覆蓋(注意後端模塊不要覆蓋string方法,後臺須要接收單引號),同理也能夠在這裏自定義過濾器:
php過濾器:http://php.net/manual/en/filter.filters.sanitize.phphtml

$di->set('filter', function ()  {
    $filter = new \Phalcon\Filter();

    /*
     *
     *重寫string過濾器:原string過濾器沒有去除單引號, 原string的zep代碼:filter_var(value, FILTER_SANITIZE_STRING);
     * 過濾器文檔:http://php.net/manual/en/filter.filters.sanitize.php
    */
    $filter->add(
        "string",
        function ($value) {
            return filter_var($value, FILTER_SANITIZE_FULL_SPECIAL_CHARS); //等同調用了htmlspecialchars() 
        }
    );

    return $filter;

}, true);
相關文章
相關標籤/搜索