PHP Form Sensitive character filter class, PHP form filter _php Tutorial

Source: Internet
Author: User
Tags php form sprintf

PHP Form Sensitive character filter class, PHP form filter


This article describes the php form sensitive character filter class and its usage. Share to everyone for your reference. The specific analysis is as follows:

Copy CodeThe code is as follows:
/**
* Form Generation Verification File
*/
$_form = new Formhtmlfind ();
Class formhtmlfind{
/**
* Output Form function
* $formKey Form Key
* An array of original information when $infoArray update
*/

Public Function formhtml ($array, $infoArray = ")
{
Detects if an array exists
if (Emptyempty ($array)) return false;
$newform = null;
Information Array (update information)
$this->infoarray =!emptyempty ($infoArray)? $infoArray: Array ();
$this->array[' class '] = Get_class_methods (Get_class ());
foreach ($array as $key = $arr)
{
Key value converted to plain English
$key = Preg_replace ("/[^a-z]/i", "', $key);
Generate form
$newform. = $this->outputform ($arr, $key);
}
Output form
Return $newform. $this->jserror ();
}
/**
* Generate Form functions
*/
Private Function Outputform ($arr, $key)
{
$value = null;
if (Emptyempty ($arr)) return false;
Input Type
$type = $key;
Input NAME
$name = Trim ($arr [0]);
Input initial value does not contain multiple selections, single class
$value = (!emptyempty ($this->infoarray[$name])? Trim ($this->infoarray[$name]): Trim ($arr [1]);
$value = Emptyempty ($this->post[$name])? $value: Trim ($this->post[$name]);
Input Title
$title = Trim ($arr [2]);
Style
$style = Trim ($arr [3]);
if ($key!== "hidden")
{
$dt = "

{$title}
";
JS Error hints
$DD = "
RN ";
}
Return (!preg_match ("/checkbox|select|radio/i", $key))?
$dt. $this->newinput ($type, $name, $value, $style, $title). $DD:
$this->formselect ($type, $name, $arr [1], $title, $style); Multi-Select Class
}
/**
* Submit Data detection
*/
Public Function Postform ($array)
{
Detects if an array exists
if (Emptyempty ($array) | | Emptyempty ($_post)) return false;
$this->post = $_post;
$this->array[' class '] = Get_class_methods (Get_class ());
foreach ($array as $key = $arr)
{
Key value converted to plain English
$key = Preg_replace ("/[^a-z]/i", "', $key);
Detect logoff File class form
if (!emptyempty ($arr) && ' file '! = $key) $newData [Trim ($arr [0])] = $this->postfind ($arr, $key);
}
Output form
if (!emptyempty ($this->error))
{
return false;
}
else return $newData;
}
/**
* Generate form
*/
Private Function Newinput ($type, $name, $value, $style, $title)
{
Switch ($type)
{
Case ' text ':
Single-line text
Return " ";
Break
Case ' password ':
Password input
Return " ";
Break
Case ':
Multiple lines of text
Return " <textarea name="{$name}" {$style}=""></textarea>{$value}";
Break
Case ' hidden ':
Hide
Return " ";
Break
Case ' file ':
File Upload
Return " ";
Break
Case ' Submit ':
Submit
Return " ";
Break
Default
Return "{$type} type error!!!";
Break
}
}
/**
* Submit Information detection
* Errors returned error
*/
Private Function Postfind ($arr, $key)
{
if (Emptyempty ($arr)) return false;
$name = $title = $error = $find = $standard =null;
Input NAME
$name = Trim ($arr [0]);
Input Title
$title = Trim ($arr [2]);
Error hints
$error = Trim ($arr [4]);
Detection type Y N
$find = Trim ($arr [5]);
Testing standards
$standard = Trim ($arr [6]);
//
if (!emptyempty ($standard)) $this->error. = $this->ck_split ($standard, $name, $title, $find, $error);
Convert to String
if (Is_array ($this->post[$name]) $this->post[$name] = Implode (",", $this->post[$name]);
Escape or other conversions
$KKarray = Array ();
if (Preg_match ("/y| N/is ", $find))
{
$KKarray = Split ("_", $find);
Escape or filter
$escape _filter = (!emptyempty ($KKarray [1]))? ' Ck_ '. $KKarray [1]: ';
Output of legitimate data through detection
$data = ($escape _filter)? $this, $escape _filter ($this->post[$name]): $this->post[$name];

}
else $data = "";
To output new data
return $data;
}
/**
* Multi-Select type form generation
*/
Private Function Formselect ($type, $name, $value, $title, $style)
{
$outform = null;
Initial when triggering an update and commit action
$nowvalue = (!emptyempty ($this->post[$name])) $this->post[$name]: $this->infoarray[$name];
Compatible with multi-select recognition, to array
if (!emptyempty ($nowvalue)) $valueArray = Explode (",", $nowvalue);
Option title
if (Is_array ($title))
{
Array_unshift ($title, ' Choice ');
$titarray = Array_values ($title);
}else $titarray = explode ("|", $title);
Option value
if (Is_array ($value))
{
Array_unshift ($value, ' Choice ');
$valarray = Array_keys ($value);
if (Emptyempty ($title)) $titarray = Array_values ($value);
}
else $valarray = explode ("|", $value);
Cancel the initial default value of the form
if (!emptyempty ($this->post) &&!emptyempty ($this->infoarray)) $value = Preg_replace ("/y_/i", ", $value );

foreach ($valarray as $key = $varl)
{
Non-default recognition
if (!emptyempty ($valueArray)) $select = (In_array ($varl, $valueArray))? ' Y ': ';
Determines whether the default
else $select = (eregi ("Y_", $varl))? ' Y ': ';

if ($key > ' 0 ')
{
$_title= ($titarray [$key])? $titarray [$key]: $title;
Switch ($type)
{
Case ' SELECT ':
if (' Y ' = = $select) $select = ' selected ';
$outform. = sprintf (" %sRn
, $select, Preg_replace ("/y_/i", "', $varl), $_title);
Break
Case ' Radio ':
if (' Y ' = = $select) $select = ' checked ';
$outform. = sprintf (" %sRN ",
$_title, $select, $name, $varl, $style);
Break
Case ' checkbox ':
if (' Y ' = = $select) $select = ' checked ';
$outform. = sprintf (" %sRN ", $_title, $select, $name, $varl, $style);
Break
}
$select =null;
}
}
Drop-down selection
if ($type = = ' SELECT ') $outform = sprintf (' %s', $name, $style, $outform);
Return sprintf ("
%s
%s
RN ", $titarray [0], $outform, $name);
}
/**
* Form validation and all Ck_ class functions
*/
Private Function Ck_split ($standard, $name, $title, $find, $error)
{
Non-mandatory default Skip
if (eregi (' N ', $find) && emptyempty ($this->post[$name])) return false;
Required Default Detection
if (eregi (' Y ', $find) && emptyempty ($this->post[$name])) return "[" j{$name} "," $error "],";
$t _error = null;
Multiple tests
$arr = Explode (', ', $standard);
Post Data detection
if (!emptyempty ($arr)) foreach ($arr as $var)
{
if (Trim ($var)! = ")
{
Switch ($this->post)
{
Case Is_array ($this->post[$name]):
Detection of array classes
foreach ($this->post[$name] as $_var)
{
$t _error.= ($this->ck_open ($_var,trim ($var))) "": $error;
if ($t _error) break;
}
Break
Default
$t _error.= ($this->ck_open ($this->post[$name],trim ($var)))? ": $error;
Break
}
if ($t _error) break;
}
}
Return ($t _error)? "[" J{$name} "," $t _error "],": "";
}
Function call
Private Function Ck_open ($string, $STR)
{
$functi = $this->ck_detected ($STR);
Return ($this-$functi ($string, $str))? True:false;
}
Type judgment
Private Function ck_detected ($STR)
{
$detect = (eregi ("^[a-za-z]*$", $str))? "{$str}detect": ' Lengthdetect ';
if (!in_array ($detect, $this->array[' class '))
{
Location (' index.php ', $ck, ' Lack of function!!! ');
}
return $detect;
}
-------------------------------------the following is an external call to the detection function
Length
Public Function Lengthdetect ($string, $str) {
$len = Split ('-', trim ($STR));
Return (strlen ($string) > ($len [0]-1) && strlen ($string) < ($len [1]+1))? True:false;
}
Price
Public Function Moneydetect ($STR) {
Return Preg_match ("/^ (-|+) d+ (. d+)? $/", $STR);
}
Mail
Public Function Emaildetect ($STR) {
Return Preg_match ("/^w+" ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *$/", $str);
}
Url
Public Function Urldetect ($STR) {
Return Preg_match ("/^http://[a-za-z0-9]+.[ a-za-z0-9]+[/=?%-&_~ ' @[] ': +!] * ([^<> "]) *$/", $str);
}
Digital type
Public Function Numdetect ($STR) {
Return Is_numeric ($STR);
}
Chinese
Public Function Cndetect ($STR) {
Return Preg_match ("/^[x7f-xff]+$/", $str);
}
Letters
Public Function Endetect ($STR) {
Return Preg_match ("/^[a-za-z]+$/", $str);
}
Mixed Digital Letters
Public Function Numendetect ($STR) {
Return Preg_match ("/^ ([a-za-z0-9_-]) +$/", $str);
}
Phone number
Public Function Teldetect ($STR) {
Return Ereg ("^[+]?[ 0-9]+ ([xx-][0-9]+) *$ ", $str);
}
Sensitive words
Public Function Keydetect ($STR) {
Return (!preg_match ("/$badkey/I", $str));
}
-----------------------------------------------------output
Character substitution
Public Function Ck_filter ($STR) {
$str = (Is_array ($str))? Implode (",", $str): $str;
$str =nl2br ($STR); Replace the carriage return with the

$str =htmlspecialchars ($STR); Converts special characters to HTML format.
$str =str_replace (Array ("", ' <? '), Array ("", ' <? '), $STR); Replace a space with
return $str;
}
Escape
function Ck_escape ($STR)
{
if (!GET_MAGIC_QUOTES_GPC ()) return addslashes ($STR);
return $str;
}
MD5 encryption
Public Function Ck_md5 ($STR) {
Return MD5 ($STR);
}
Base64 encryption
Public Function Ck_base64 ($STR) {
Return Base64_encode ($STR);
}
Time
function Ck_time ($STR) {
Time_r () to the common function file
if (!is_numeric ($STR))
{
Return Time_r ($STR);
}
else return $str;
}
Conditional logoff (number)
Public Function Ck_cancel ($STR) {
Return (!is_numeric ($STR))? $STR: "";
}
Unconditional logoff
Public Function Ck_delete () {
return null;
}
JS Error hints
Private Function Jserror ()
{
if (Emptyempty ($this->error)) return false;
Return "

";
}
}

Demonstrate:
$form [1] =array (
' Text ' =>array (' title ', ' ', ' Product name ', ' size=40 ', ' Product name indispensable! ', ' Y ', ' cn,1-30 '),
' Text1 ' =>array (' categories ', ' ', ' Product name ', ' ', ' ', ' y_base64 '),
' Select ' =>array (' superiors ', ' | | 1|2| Y_3 ', ' Product category | Select |1|2|3 ', ' ', ' required ', ' Y ',
' Radio ' =>array (' superiors1 ', ' |1| Y_2|3 ', ' Product xun| product 1| product 2| Product 3 ', ' ', ' required option ', ' Y '),
' checkbox ' =>array (' superiors2 ', Array (1=> ',2=> ' of ',3=> ' 33 '), ' ', ' ', ' required ', ' Y '),
' File ' =>array (' ddd ', ' ', ' files '),
);
$form =array (
' Login ' =
Array (
' Text ' = =
Array (
0 = ' user ',
1 = ",
2 = ' User name ',
3 = ' size=20 ',
4 = '! ',
5 = ' Y ',
6 = ' numen,6-12 ',
),
' Password ' =
Array (
0 = ' Pass ',
1 = ",
2 = ' Password ',
3 = ' size=22 ',
4 = ' password format is wrong! ',
5 = ' y_md5 ',
6 = ' numen,6-12 ',
),
' Radio ' =
Array (
0 = ' time ',
1 = ' |7200|3600|1800 ',
2 = ' Cookies are valid for hours------hours |30 minutes ',
3 = ",
4 = ",
5 = ' N_delete ',
6 = ",
),
),
);

Form submission Validation
$past = $_form->postform ($form [' Login ']);
$DD = Array (' title ' = ' + ' headline ', ' categories ' = ' category ');
$dd The post data bit internal processing has precedence for existing information (such as information output at update)
if (!emptyempty ($past))
{
echo "


Echo
";
}
Echo ';

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/923907.html www.bkjia.com true http://www.bkjia.com/PHPjc/923907.html techarticle PHP form Sensitive character filter class, PHP form Filter This article describes the php form sensitive character filter class and its usage. Share to everyone for your reference. The specific analysis is as follows: Copy code ...

  • 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.