Data filtering, formatting user input PHP notes

Source: Internet
Author: User
Data filtering, formatting user input PHP notes
1. Filtering
Abstract class filter{
Protected $blackstr = Array ();
Protected $whitestr = Array ();

Abstract function Filtit ($STR);
}
Special symbols for filtering user names
Class Loginfilter extends Filter {
function Filtit ($STR) {
$this->blackstr = Array ("/[\x7f-\xff]/", "/\w/");
Return Preg_replace ($this->blackstr, "", $str);
}
}
Filter the contents of the Input text box
Class Editfilter extends Filter {
function Filtit ($STR) {
$this->blackstr = Array ("/\&/", "/\"/","/\ "/", "/\ /",
"/\\\\/", "/\//", "/-/", "/\*/", "/ /" );
$this->whitestr = Array ("&", "'", "<", ">", "\" ","/","-"," * "," "");
Return Preg_replace ($this->blackstr, $this->whitestr, $STR);
}
}



2.//user's message is a piece of code: for example, JS script, so how to avoid the harm of data in time, the data is displayed correctly

$js = "";//Assume that the user entered a script
$str 1 = base64_encode ($JS);//Encode data using MIME base64
echo $str 1. "
";

$str 2 = Base64_decode ($str 1);//
echo Htmlspecialchars ($JS);//convert special characters to HTML character encoding
  • Related Article

    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.