Several examples of php character string filtering

Source: Internet
Author: User
Tags explode trim

Example

We use preg_replace and str_ireplace to replace

The code is as follows: Copy code

Public static function filterStr ($ value)
{
If (empty ($ value ))
{
Return "";
}
$ Value = trim ($ value );
$ Badstr = array ("x00", "% 00", "r", "&", "," '"," <","> ", "% 3C", "% 3E ");
$ Newstr = array ("", "& amp;", "& quot;", "& #39;", "& lt ;", "& gt;", "& lt;", "& gt ;");
$ Value = str_ireplace ($ badstr, $ newstr, $ value );
$ Value = preg_replace ("/& amp; (# (d {3, 5} | x [a-fA-F0-9] {4});)/", "& 1 ", $ value );
Return $ value;
}
Public static function stripArray (& $ _ data)
{
If (is_array ($ _ data ))
{
Foreach ($ _ data as $ _ key = >$ _ value)
{
$ _ Data [$ _ key] = trim (self: striparray ($ _ value ));
}
Return $ _ data;
}
Return stripslashes (trim ($ _ data ));
}

Add to favorites:

The code is as follows: Copy code

<? Php
Class XRequest
{
 
Public static function getPost ($ name = "")
{
If (empty ($ name ))
{
Return $ _ POST;
}
If (isset ($ _ POST [$ name])
{
Return $ _ POST [$ name];
}
Return "";
}
 
Public static function getGet ($ name = "")
{
If (empty ($ name ))
{
Return $ _ GET;
}
If (isset ($ _ GET [$ name])
{
Return $ _ GET [$ name];
}
Return "";
}
 
Public static function getCookie ($ name = "")
{
If ($ name = "")
{
Return $ _ COOKIE;
}
If (isset ($ _ COOKIE [$ name])
{
Return $ _ COOKIE [$ name];
}
Return "";
}
 
Public static function getSession ($ name = "")
{
If ($ name = "")
{
Return $ _ SESSION;
}
If (isset ($ _ SESSION [$ name])
{
Return $ _ SESSION [$ name];
}
Return "";
}
 
Public static function fetchEnv ($ name = "")
{
If ($ name = "")
{
Return $ _ ENV;
}
If (isset ($ _ ENV [$ name])
{
Return $ _ ENV [$ name];
}
Return "";
}
 
Public static function getService ($ name = "")
{
If ($ name = "")
{
Return $ _ SERVER;
}
If (isset ($ _ SERVER [$ name])
{
Return $ _ SERVER [$ name];
}
Return "";
}
 
Public static function getPhpSelf ()
{
Return strip_tags (self: getservice ("PHP_SELF "));
}
 
Public static function getServiceName ()
{
Return self: getservice ("SERVER_NAME ");
}
 
Public static function getRequestTime ()
{
Return self: getservice ("REQUEST_TIME ");
}
 
Public static function getUserAgent ()
{
Return self: getservice ("HTTP_USER_AGENT ");
}
 
Public static function getUri ()
{
Return self: getservice ("REQUEST_URI ");
}
 
Public static function isPost ()
{
If (strtolower (self: getservice ("REQUEST_METHOD") = "post ")
{
Return TRUE;
}
Return FALSE;
}
 
Public static function isGet ()
{
If (strtolower (self: getservice ("REQUEST_METHOD") = "get ")
{
Return TRUE;
}
Return FALSE;
}
 
Public static function isAjax ()
{
If (self: getservice ("HTTP_X_REQUESTED_WITH") & strtolower (self: getservice ("HTTP_X_REQUESTED_WITH") = "xmlhttprequest ")
{
Return TRUE;
}
If (self: getservice ("HTTP_REQUEST_TYPE") & strtolower (self: getservice ("HTTP_REQUEST_TYPE") = "ajax ")
{
Return TRUE;
}
If (self: getpost ("oe_ajax") | self: getget ("oe_ajax "))
{
Return TRUE;
}
Return FALSE;
}
 
Public static function getip ()
{
Static $ realip = NULL;
If (isset ($ _ SERVER ))
{
If (isset ($ _ SERVER ['http _ X_FORWARDED_FOR '])
{
$ Realip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
}
Else if (isset ($ _ SERVER ['http _ CLIENT_IP '])
{
$ Realip = $ _ SERVER ['http _ CLIENT_IP '];
}
Else
{
$ Realip = $ _ SERVER ['remote _ ADDR '];
}
}
Else if (getenv ("HTTP_X_FORWARDED_FOR "))
{
$ Realip = getenv ("HTTP_X_FORWARDED_FOR ");
}
Else if (getenv ("HTTP_CLIENT_IP "))
{
$ Realip = getenv ("HTTP_CLIENT_IP ");
}
Else
{
$ Realip = getenv ("REMOTE_ADDR ");
}
$ One = "([0-9] | [0-9] {2} | 1dd | 2 [0-4] d | 25 [0-5])";
If (! @ Preg_match ("/". $ one. ".". $ one. "$/", $ realip ))
{
$ Realip = "0.0.0.0 ";
}
Return $ realip;
}
 
Protected static function uri ()
{
$ Uri = self: geturi ();
$ File = dirname ($ _ SERVER ['script _ name']);
$ Request = str_replace ($ file, "", $ uri );
$ Request = explode ("/", trim ($ request ,"/"));
If (isset ($ request [0])
{
$ GLOBALS ['_ get'] ['c'] = $ request [0];
Unset ($ request [0]);
}
If (isset ($ request [1])
{
$ GLOBALS ['_ get'] ['A'] = $ request [1];
Unset ($ request [1]);
}
If (1 <count ($ request ))
{
$ Mark = 0;
$ Val = $ key = array ();
Foreach ($ request as $ value)
{
+ + $ Mark;
If ($ mark % 2 = 0)
{
$ Val [] = $ value;
}
Else
{
$ Key [] = $ value;
}
}
If (count ($ key )! = Count ($ val ))
{
$ Val [] = NULL;
}
$ Get = array_combine ($ key, $ val );
Foreach ($ get as $ key => $ value)
{
$ GLOBALS ['_ get'] [$ key] = $ value;
}
}
Return TRUE;
}
 
Public static function getGpc ($ value, $ isfliter = TRUE)
{
If (! Is_array ($ value ))
{
If (isset ($ _ GET [$ value])
{
$ Temp = trim ($ _ GET [$ value]);
}
If (isset ($ _ POST [$ value])
{
$ Temp = trim ($ _ POST [$ value]);
}
$ Temp = $ isfliter === TRUE? XFilter: filterstr ($ temp): $ temp;
Return trim ($ temp );
}
$ Temp = array ();
Foreach ($ value as $ val)
{
If (isset ($ _ GET [$ val])
{
$ Temp [$ val] = trim ($ _ GET [$ val]);
}
If (isset ($ _ POST [$ val])
{
$ Temp [$ val] = trim ($ _ POST [$ val]);
}
$ Temp [$ val] = $ isfliter === TRUE? XFilter: filterstr ($ temp [$ val]): $ temp [$ val];
}
Return $ temp;
}
 
Public static function getArgs ($ value, $ default = NULL, $ isfliter = TRUE)
{
If (! Empty ($ value ))
{
If (isset ($ _ GET [$ value])
{
$ Temp = trim ($ _ GET [$ value]);
}
If (isset ($ _ POST [$ value])
{
$ Temp = trim ($ _ POST [$ value]);
}
If ($ isfliter)
{
$ Temp = XFilter: filterstr ($ temp );
}
Else
{
$ Temp = XFilter: striparray ($ temp );
}
If (empty ($ temp )&&! Empty ($ default ))
{
$ Temp = $ default;
}
Return trim ($ temp );
}
Return "";
}
 
Public static function getInt ($ value, $ default = NULL)
{
If (! Empty ($ value ))
{
If (isset ($ _ GET [$ value])
{
$ Temp = $ _ GET [$ value];
}
If (isset ($ _ POST [$ value])
{
$ Temp = $ _ POST [$ value];
}
$ Temp = XFilter: filterstr ($ temp );
If (empty ($ temp) | FALSE ===xvalid: isnumber ($ temp ))
{
If (TRUE = XValid: isnumber ($ default ))
{
$ Temp = $ default;
}
Else
{
$ Temp = 0;
}
}
Return intval ($ temp );
}
Return 0;
}
 
Public static function getArray ($ value)
{
If (! Empty ($ value ))
{
If (isset ($ _ GET [$ value])
{
$ Temp = $ _ GET [$ value];
}
If (isset ($ _ POST [$ value])
{
$ Temp = $ _ POST [$ value];
}
Return $ temp;
}
Return "";
}
 
Public static function recArgs ($ value)
{
If (! Empty ($ value ))
{
If (isset ($ _ GET [$ value])
{
$ Temp = $ _ GET [$ value];
}
If (isset ($ _ POST [$ value])
{
$ Temp = $ _ POST [$ value];
}
Return XFilter: filterbadchar ($ temp );
}
Return "";
}
 
Public static function getComArgs ($ itemname)
{
$ Args = "";
$ Array = self: getarray ($ itemname );
If (! Empty ($ array ))
{
$ Ii = 0;
For (; $ ii <count ($ array); ++ $ ii)
{
$ Val = XFilter: filterbadchar ($ array [$ ii]);
If (! Empty ($ val ))
{
If ($ ii = 0)
{
$ Args = $ val;
}
Else if ($ args = "")
{
$ Args = $ val;
}
Else
{
$ Args = $ args. ",". $ val;
}
}
}
}
Return $ args;
}
 
Public static function getComInts ($ name)
{
$ Args = "";
$ Array = self: getarray ($ name );
If (! Empty ($ array ))
{
$ Ii = 0;
For (; $ ii <count ($ array); ++ $ ii)
{
$ Val = intval (XFilter: filterbadchar ($ array [$ ii]);
If (! Empty ($ val ))
{
If ($ ii = 0)
{
$ Args = $ val;
}
Else if ($ args = "")
{
$ Args = $ val;
}
Else
{
$ Args = $ args. ",". $ val;
}
}
}
}
Return $ args;
}
 
}
 
If (! Defined ("IN_OESOFT "))
{
Exit ("Access Denied ");
}
?>
<? Php
Class XFilter
{
 
Public static function filterBadChar ($ str)
{
If (empty ($ str) | $ str = "")
{
Return;
}
$ Badstring = array ("'", "=", "#", "$", ">", "<","", "/*", "%", "x00", "% 00 ","*");
$ Newstring = array ("","","","","","","","","","","", "","","");
$ Str = str_replace ($ badstring, $ newstring, $ str );
Return trim ($ str );
}
 
Public static function stripArray (& $ _ data)
{
If (is_array ($ _ data ))
{
Foreach ($ _ data as $ _ key = >$ _ value)
{
$ _ Data [$ _ key] = trim (self: striparray ($ _ value ));
}
Return $ _ data;
}
Return stripslashes (trim ($ _ data ));
}
 
Public static function filterSlashes (& $ value)
{
If (get_magic_quotes_gpc ())
{
Return FALSE;
}
$ Value = (array) $ value;
Foreach ($ value as $ key => $ val)
{
If (is_array ($ val ))
{
Self: filterslashes ($ value [$ key]);
}
Else
{
$ Value [$ key] = addslashes ($ val );
}
}
}
 
Public static function filterScript ($ value)
{
If (empty ($ value ))
{
Return "";
}
$ Value = preg_replace ("/(javascript :)? On (click | load | key | mouse | error | abort | move | unload | change | dblclick | move | reset | resize | submit)/I "," & 111n2 ", $ value );
$ Value = preg_replace ("/<script (. *?)> (.*?) </Script>/si "," ", $ value );
$ Value = preg_replace ("/<iframe (. *?)> (.*?) </Iframe>/si "," ", $ value );
$ Value = preg_replace ("/<object. + </object>/iesU", "", $ value );
Return $ value;
}
 
Public static function filterHtml ($ value)
{
If (empty ($ value ))
{
Return "";
}
If (function_exists ("htmlspecialchars "))
{
Return htmlspecialchars ($ value );
}
Return str_replace (array ("&", "," '"," <","> "), array (" & amp; "," & quot ;", "& #039;", "& lt;", "& gt;"), $ value );
}
 
Public static function filterSql ($ value)
{
If (empty ($ value ))
{
Return "";
}
$ SQL = array ("select", "insert", "update", "delete ","'","/*",".. /",". /"," union "," into "," load_file "," outfile ");
$ SQL _re = array ("","","","","","","","","","","", "");
Return str_ireplace ($ SQL, $ SQL _re, $ value );
}
 
Public static function filterStr ($ value)
{
If (empty ($ value ))
{
Return "";
}
$ Value = trim ($ value );
$ Badstr = array ("x00", "% 00", "r", "&", "," '"," <","> ", "% 3C", "% 3E ");
$ Newstr = array ("", "& amp;", "& quot;", "& #39;", "& lt ;", "& gt;", "& lt;", "& gt ;");
$ Value = str_ireplace ($ badstr, $ newstr, $ value );
$ Value = preg_replace ("/& amp; (# (d {3, 5} | x [a-fA-F0-9] {4});)/", "& 1 ", $ value );
Return $ value;
}
 
Public static function filterUrl ()
{
If (preg_replace ("/https? : // ([^:/] +). */I "," 1 ", $ _ SERVER ['http _ referer'])! = Preg_replace ("/([^:] +). */", "1", $ _ SERVER ['http _ host'])
{
Return FALSE;
}
Return TRUE;
}
 
Public static function filterForbidChar ($ content)
{
$ New_content = $ content;
$ Forbidargs = X: $ cfg ['forbidargs '];
If (! Empty ($ forbidargs ))
{
$ Array = explode (",", $ forbidargs );
$ I = 0;
For (; $ I <sizeof ($ array); ++ $ I)
{
$ New_content = str_ireplace ($ array [$ I], "", $ content );
}
}
Return $ new_content;
}
 
Public static function checkExistsForbidChar ($ content)
{
$ Flag = FALSE;
$ Forbidargs = X: $ cfg ['forbidargs '];
If (! Empty ($ forbidargs ))
{
$ Array = explode (",", $ forbidargs );
$ I = 0;
For (; $ I <sizeof ($ array); ++ $ I)
{
If (FALSE === strpos (strtolower ($ content), strtolower ($ array [$ I])
{
Continue;
}
$ Flag = TRUE;
Break;
}
}
Return $ flag;
}
 
Public static function checkExistsForbidUserName ($ username)
{
$ Flag = FALSE;
$ Forbidargs = X: $ cfg ['lockusers'];
If (! Empty ($ forbidargs ))
{
$ Array = explode (",", $ forbidargs );
$ I = 0;
For (; $ I <sizeof ($ array); ++ $ I)
{
If (FALSE === strpos (strtolower ($ username), strtolower ($ array [$ I])
{
Continue;
}
$ Flag = TRUE;
Break;
}
}
Return $ flag;
}
 
}
 
If (! Defined ("IN_OESOFT "))
{
Exit ("Access Denied ");
}
?>

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.