_php example of the specific implementation of filtering illegal characters in PHP

Source: Internet
Author: User
Tags sql injection
Copy code code as follows:

<?php
class Sqlsafe {
Private $getfilter = ' | And|or) \\b.+? (>|<|=|in|like) |\\/\\*.+?\\*\\/|<\\s*script\\b|\\bexec\\b| Union.+? Select| Update.+? Set| Insert\\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \\s+ (table| DATABASE) ";
Private $postfilter = \\b (and|or) \\b.{1,6}? (=|>|<|\\bin\\b|\\blike\\b) |\\/\\*.+?\\*\\/|<\\s*script\\b|\\bexec\\b| Union.+? Select| Update.+? Set| Insert\\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \\s+ (table| DATABASE) ";
Private $cookiefilter = \\b (and|or) \\b.{1,6}? (=|>|<|\\bin\\b|\\blike\\b) |\\/\\*.+?\\*\\/|<\\s*script\\b|\\bexec\\b| Union.+? Select| Update.+? Set| Insert\\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \\s+ (table| DATABASE) ";
/**
* constructor
/
Public Function __construct () {
foreach ($_get as $key => $value) {$this->stop Attack ($key, $value, $this->getfilter);
ForeaCH ($_post as $key => $value) {$this->stopattack ($key, $value, $this->postfilter);}
foreach ($_cookie as $key => $value) {$this->stopattack ($key, $value, $this->cookiefilter);
}
/**
* parameter check and write log
*/
Public Function Stopattack ($StrFiltKey, $StrFiltValue, $ArrFiltReq) {
if (Is_array ($StrFiltValue)) $StrFiltValue = implode ($StrFiltValue);
if (Preg_match ("/". $ArrFiltReq. " /is ", $StrFiltValue) = = 1) {
$this->writeslog ($_server[" REMOTE_ADDR "]." ". Strftime ("%y-%m-%d%h:%m:%s ")." ". $_server[" Php_self "]." ". $_server[" Request_method "]." ". $StrFiltKey." ". $StrFiltValue);
ShowMsg (' The parameter you submitted is illegal, the system has logged your operation! ', ', 0, 1;
}
}
/**
* SQL injection log
/
Public Function Writeslog ($log) {
$log _path = cache_path. ' Logs '. Directory_separator. ' Sql_log.txt ';
$ts = fopen ($log _path, "A +");
Fputs ($ts, $log.) \ r \ n ");
Fclose ($ts);
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.