Data filtering and formatting php notes for user input

Source: Internet
Author: User
Data filtering and formatting user input php Notes 1. filter abstractclassFilter {& nbsp; protected $ blackstr = array (); & nbsp; & nb data filtering and formatting user input php notes
1. filter
Abstract class Filter {
Protected $ blackstr = array ();
Protected $ whitestr = array ();

Abstract function filtit ($ str );
}
// Filter the special characters of the user name
Class LoginFilter extends Filter {
Function filtit ($ str ){
$ This-> blackstr = array ("/[\ x7f-\ xff]/", "/\ W /");
Return preg_replace ($ this-> blackstr, "", $ str );
}
}
// Filter the input text box content
Class EditFilter extends Filter {
Function filtit ($ str ){
$ This-> blackstr = array ("/\&/","/\"/","/\"/","/\ /",
"/\\\\/","/\//","/-/","/\*/","//");
$ This-> whitestr = array ("&", "'", "<", "> ","\'","/","-", "*","");
Return preg_replace ($ this-> blackstr, $ this-> whitestr, $ str );
}
}



2. // The user's message is a piece of code, such as a js script. how can we avoid the dangers of data in time and display the data correctly when it is retrieved?

$ Js = "script" alert ('Look me'); script "; // assume that the user inputs a script.
// $ Str1 = base64_encode ($ js); // encode the data using MIME base64
// Echo $ str1 ."
";

// $ Str2 = base64_decode ($ str1 );//
Echo htmlspecialchars ($ js); // Convert Special characters to HTML Character encoding

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.