首页 > 开发 > PHP > 正文

整理了php过滤字符串几个例子

2024-05-04 22:00:00
字体:大 中 小
来源:转载
供稿:网友

php中过滤一些特殊字符我们通常用于在安全数据提交或者敏感词的过滤上,下面整理了一些常用的例子供大家参考,有需要了可进入参考.

例子,我们利用preg_replace与str_ireplace来进行替换操作,代码如下:

  1. public static function filterStr( $value ) 
  2. { 
  3. if ( emptyempty( $value ) ) 
  4. { 
  5. return ""; 
  6. } 
  7. $value = trim( $value ); 
  8. $badstr = array( "x00", "%00", "r", "&", """, "'", "<", ">", "%3C", "%3E" ); 
  9. $newstr = array( "", "", "", "&amp;", "&quot;", "&#39;", "&lt;", "&gt;", "&lt;", "&gt;" ); 
  10. $value = str_ireplace( $badstr, $newstr, $value ); 
  11. $value = preg_replace( "/&amp;((#(d{3,5}|x[a-fA-F0-9]{4}));)/", "&1", $value ); 
  12. return $value; 
  13. } 
  14. public static function stripArray( &$_data ) 
  15. { 
  16. if ( is_array( $_data ) ) 
  17. { 
  18. foreach ( $_data as $_key => $_value ) 
  19. { 
  20. $_data[$_key] = trim( self::striparray( $_value ) ); 
  21. } 
  22. return $_data; 
  23. } 
  24. return stripslashes( trim( $_data ) ); 
  25. } 

另收藏代码如下:

  1. <?php 
  2. class XRequest 
  3. { 
  4.  
  5. public static function getPost( $name = "" ) 
  6. { 
  7. if ( emptyempty( $name ) ) 
  8. { 
  9. return $_POST; 
  10. } 
  11. if ( isset( $_POST[$name] ) ) 
  12. { 
  13. return $_POST[$name]; 
  14. } 
  15. return ""; 
  16. } 
  17.  
  18. public static function getGet( $name = "" ) 
  19. { 
  20. if ( emptyempty( $name ) ) 
  21. { 
  22. return $_GET; 
  23. } 
  24. if ( isset( $_GET[$name] ) ) 
  25. { 
  26. return $_GET[$name]; 
  27. } 
  28. return ""; 
  29. } 
  30.  
  31. public static function getCookie( $name = "" ) 
  32. { 
  33. if ( $name == "" ) 
  34. { 
  35. return $_COOKIE; 
  36. } 
  37. if ( isset( $_COOKIE[$name] ) ) 
  38. { 
  39. return $_COOKIE[$name]; 
  40. } 
  41. return ""; 
  42. } 
  43.  
  44. public static function getSession( $name = "" ) 
  45. { 
  46. if ( $name == "" ) 
  47. { 
  48. return $_SESSION; 
  49. } 
  50. if ( isset( $_SESSION[$name] ) ) 
  51. { 
  52. return $_SESSION[$name]; 
  53. } 
  54. return ""; 
  55. } 
  56.  
  57. public static function fetchEnv( $name = "" ) 
  58. { 
  59. if ( $name == "" ) 
  60. { 
  61. return $_ENV; 
  62. } 
  63. if ( isset( $_ENV[$name] ) ) 
  64. { 
  65. return $_ENV[$name]; 
  66. } 
  67. return ""; 
  68. } 
  69.  
  70. public static function getService( $name = "" ) 
  71. { 
  72. if ( $name == "" ) 
  73. { 
  74. return $_SERVER; 
  75. } 
  76. if ( isset( $_SERVER[$name] ) ) 
  77. { 
  78. return $_SERVER[$name]; 
  79. } 
  80. return ""; 
  81. } 
  82.  
  83. public static function getPhpSelf( ) 
  84. { 
  85. return strip_tags( self::getservice( "PHP_SELF" ) ); 
  86. } 
  87.  
  88. public static function getServiceName( ) 
  89. { 
  90. return self::getservice( "SERVER_NAME" ); 
  91. } 
  92.  
  93. public static function getRequestTime( ) 
  94. { 
  95. return self::getservice( "REQUEST_TIME" ); 
  96. } 
  97.  
  98. public static function getUserAgent( ) 
  99. { 
  100. return self::getservice( "HTTP_USER_AGENT" ); 
  101. } 
  102.  
  103. public static function getUri( ) 
  104. { 
  105. return self::getservice( "REQUEST_URI" ); 
  106. } 
  107.  
  108. public static function isPost( ) 
  109. { 
  110. if ( strtolower( self::getservice( "REQUEST_METHOD" ) ) == "post" ) 
  111. { 
  112. return TRUE; 
  113. } 
  114. return FALSE; 
  115. } 
  116.  
  117. public static function isGet( ) 
  118. { 
  119. if ( strtolower( self::getservice( "REQUEST_METHOD" ) ) == "get" ) 
  120. { 
  121. return TRUE; 
  122. } 
  123. return FALSE; 
  124. } 
  125.  
  126. public static function isAjax( ) 
  127. { 
  128. if ( self::getservice( "HTTP_X_REQUESTED_WITH" ) && strtolower( self::getservice( "HTTP_X_REQUESTED_WITH" ) ) == "xmlhttprequest" ) 
  129. { 
  130. return TRUE; 
  131. } 
  132. if ( self::getservice( "HTTP_REQUEST_TYPE" ) && strtolower( self::getservice( "HTTP_REQUEST_TYPE" ) ) == "ajax" ) 
  133. { 
  134. return TRUE; 
  135. } 
  136. if ( self::getpost( "oe_ajax" ) || self::getget( "oe_ajax" ) ) 
  137. { 
  138. return TRUE; 
  139. } 
  140. return FALSE; 
  141. } 
  142.  
  143. public static function getip( ) 
  144. { 
  145. static $realip = NULL; 
  146. if ( isset( $_SERVER ) ) 
  147. { 
  148. if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) 
  149. { 
  150. $realip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
  151. } 
  152. else if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) 
  153. { 
  154. $realip = $_SERVER['HTTP_CLIENT_IP']; 
  155. } 
  156. else 
  157. { 
  158. $realip = $_SERVER['REMOTE_ADDR']; 
  159. } 
  160. } 
  161. else if ( getenv( "HTTP_X_FORWARDED_FOR" ) ) 
  162. { 
  163. $realip = getenv( "HTTP_X_FORWARDED_FOR" ); 
  164. } 
  165. else if ( getenv( "HTTP_CLIENT_IP" ) ) 
  166. { 
  167. $realip = getenv( "HTTP_CLIENT_IP" ); 
  168. } 
  169. else 
  170. { 
  171. $realip = getenv( "REMOTE_ADDR" ); 
  172. } 
  173. $one = "([0-9]|[0-9]{2}|1dd|2[0-4]d|25[0-5])"; 
  174. if ( !@preg_match( "/".$one.".".$one.".".$one.".".$one."$/", $realip ) ) 
  175. { 
  176. $realip = "0.0.0.0"; 
  177. } 
  178. return $realip; 
  179. } 
  180.  
  181. protected static function uri( ) 
  182. { 
  183. $uri = self::geturi( ); 
  184. $file = dirname( $_SERVER['SCRIPT_NAME'] ); 
  185. $request = str_replace( $file, "", $uri ); 
  186. $request = explode( "/", trim( $request, "/" ) ); 
  187. if ( isset( $request[0] ) ) 
  188. { 
  189. $GLOBALS['_GET']['c'] = $request[0]; 
  190. unset( $request[0] ); 
  191. } 
  192. if ( isset( $request[1] ) ) 
  193. { 
  194. $GLOBALS['_GET']['a'] = $request[1]; 
  195. unset( $request[1] ); 
  196. } 
  197. if ( 1 < count( $request ) ) 
  198. { 
  199. $mark = 0; 
  200. $val = $key = array( ); 
  201. foreach ( $request as $value ) 
  202. { 
  203. ++$mark; 
  204. if ( $mark % 2 == 0 ) 
  205. { 
  206. $val[] = $value; 
  207. } 
  208. else 
  209. { 
  210. $key[] = $value; 
  211. } 
  212. } 
  213. if ( count( $key ) !== count( $val ) ) 
  214. { 
  215. $val[] = NULL; 
  216. } 
  217. $get = array_combine( $key, $val ); 
  218. foreach ( $get as $key => $value ) 
  219. { 
  220. $GLOBALS['_GET'][$key] = $value; 
  221. } 
  222. } 
  223. return TRUE; 
  224. } 
  225.  
  226. public static function getGpc( $value, $isfliter = TRUE ) 
  227. { 
  228. if ( !is_array( $value ) ) 
  229. { 
  230. if ( isset( $_GET[$value] ) ) 
  231. { 
  232. $temp = trim( $_GET[$value] ); 
  233. } 
  234. if ( isset( $_POST[$value] ) ) 
  235. { 
  236. $temp = trim( $_POST[$value] ); 
  237. } 
  238. $temp = $isfliter === TRUE ? XFilter::filterstr( $temp ) : $temp; 
  239. return trim( $temp ); 
  240. } 
  241. $temp = array( ); 
  242. foreach ( $value as $val ) 
  243. { 
  244. if ( isset( $_GET[$val] ) ) 
  245. { 
  246. $temp[$val] = trim( $_GET[$val] ); 
  247. } 
  248. if ( isset( $_POST[$val] ) ) 
  249. { 
  250. $temp[$val] = trim( $_POST[$val] ); 
  251. } 
  252. $temp[$val] = $isfliter === TRUE ? XFilter::filterstr( $temp[$val] ) : $temp[$val]; 
  253. } 
  254. return $temp; 
  255. } 
  256.  
  257. public static function getArgs( $value, $default = NULL, $isfliter = TRUE ) 
  258. { 
  259. if ( !emptyempty( $value ) ) 
  260. { 
  261. if ( isset( $_GET[$value] ) ) 
  262. { 
  263. $temp = trim( $_GET[$value] ); 
  264. } 
  265. if ( isset( $_POST[$value] ) ) 
  266. { 
  267. $temp = trim( $_POST[$value] ); 
  268. } 
  269. if ( $isfliter ) 
  270. { 
  271. $temp = XFilter::filterstr( $temp ); 
  272. } 
  273. else 
  274. { 
  275. $temp = XFilter::striparray( $temp ); 
  276. } 
  277. if ( emptyempty( $temp ) && !emptyempty( $default ) ) 
  278. { 
  279. $temp = $default; 
  280. } 
  281. return trim( $temp ); 
  282. } 
  283. return ""; 
  284. } 
  285.  
  286. public static function getInt( $value, $default = NULL ) 
  287. { 
  288. if ( !emptyempty( $value ) ) 
  289. { 
  290. if ( isset( $_GET[$value] ) ) 
  291. { 
  292. $temp = $_GET[$value]; 
  293. } 
  294. if ( isset( $_POST[$value] ) ) 
  295. { 
  296. $temp = $_POST[$value]; 
  297. } 
  298. $temp = XFilter::filterstr( $temp ); 
  299. if ( emptyempty( $temp ) || FALSE === XValid::isnumber( $temp ) ) 
  300. { 
  301. if ( TRUE === XValid::isnumber( $default ) ) 
  302. { 
  303. $temp = $default; 
  304. } 
  305. else 
  306. { 
  307. $temp = 0; 
  308. } 
  309. } 
  310. return intval( $temp ); 
  311. } 
  312. return 0; 
  313. } 
  314.  
  315. public static function getArray( $value ) 
  316. { 
  317. if ( !emptyempty( $value ) ) 
  318. { 
  319. if ( isset( $_GET[$value] ) ) 
  320. { 
  321. $temp = $_GET[$value]; 
  322. } 
  323. if ( isset( $_POST[$value] ) ) 
  324. { 
  325. $temp = $_POST[$value]; 
  326. } 
  327. return $temp; 
  328. } 
  329. return ""; 
  330. } 
  331.  
  332. public static function recArgs( $value ) 
  333. { 
  334. if ( !emptyempty( $value ) ) 
  335. { 
  336. if ( isset( $_GET[$value] ) ) 
  337. { 
  338. $temp = $_GET[$value]; 
  339. } 
  340. if ( isset( $_POST[$value] ) ) 
  341. { 
  342. $temp = $_POST[$value]; 
  343. } 
  344. return XFilter::filterbadchar( $temp ); 
  345. } 
  346. return ""; 
  347. } 
  348.  
  349. public static function getComArgs( $itemname ) 
  350. { 
  351. $args = ""; 
  352. $array = self::getarray( $itemname ); 
  353. if ( !emptyempty( $array ) ) 
  354. { 
  355. $ii = 0; 
  356. for ( ; $ii < count( $array );   ++$ii   ) 
  357. { 
  358. $val = XFilter::filterbadchar( $array[$ii] ); 
  359. if ( !emptyempty( $val ) ) 
  360. { 
  361. if ( $ii == 0 ) 
  362. { 
  363. $args = $val; 
  364. } 
  365. else if ( $args == "" ) 
  366. { 
  367. $args = $val; 
  368. } 
  369. else 
  370. { 
  371. $args = $args.",".$val; 
  372. } 
  373. } 
  374. } 
  375. } 
  376. return $args; 
  377. } 
  378.  
  379. public static function getComInts( $name ) 
  380. { 
  381. $args = ""; 
  382. $array = self::getarray( $name ); 
  383. if ( !emptyempty( $array ) ) 
  384. { 
  385. $ii = 0; 
  386. for ( ; $ii < count( $array );   ++$ii   ) 
  387. { 
  388. $val = intval( XFilter::filterbadchar( $array[$ii] ) ); 
  389. if ( !emptyempty( $val ) ) 
  390. { 
  391. if ( $ii == 0 ) 
  392. { 
  393. $args = $val; 
  394. } 
  395. else if ( $args == "" ) 
  396. { 
  397. $args = $val; 
  398. } 
  399. else 
  400. { 
  401. $args = $args.",".$val; 
  402. } 
  403. } 
  404. } 
  405. } 
  406. return $args; 
  407. } 
  408.  
  409. } 
  410.  
  411. if ( !defined( "IN_OESOFT" ) ) 
  412. { 
  413. exit( "Access Denied" ); 
  414. } 
  415. ?> 
  416. <?php 
  417. class XFilter 
  418. { 
  419.  
  420. public static function filterBadChar( $str ) 
  421. { 
  422. if ( emptyempty( $str ) || $str == "" ) 
  423. { 
  424. return; 
  425. } 
  426. $badstring = array( "'", """, """, "=", "#", "$", ">", "<", "", "/*", "%", "x00", "%00", "*" ); 
  427. $newstring = array( "", "", "", "", "", "", "", "", "", "", "", "", "", "" ); 
  428. $str = str_replace( $badstring, $newstring, $str ); 
  429. return trim( $str ); 
  430. } 
  431.  
  432. public static function stripArray( &$_data ) 
  433. { 
  434. if ( is_array( $_data ) ) 
  435. { 
  436. foreach ( $_data as $_key => $_value ) 
  437. { 
  438. $_data[$_key] = trim( self::striparray( $_value ) ); 
  439. } 
  440. return $_data; 
  441. } 
  442. return stripslashes( trim( $_data ) ); 
  443. } 
  444.  
  445. public static function filterSlashes( &$value ) 
  446. { 
  447. if ( get_magic_quotes_gpc( ) ) 
  448. { 
  449. return FALSE; 
  450. } 
  451. $value = ( array )$value; 
  452. foreach ( $value as $key => $val ) 
  453. { 
  454. if ( is_array( $val ) ) 
  455. { 
  456. self::filterslashes( $value[$key] ); 
  457. } 
  458. else 
  459. { 
  460. $value[$key] = addslashes( $val ); 
  461. } 
  462. } 
  463. } 
  464.  
  465. public static function filterScript( $value ) 
  466. { 
  467. if ( emptyempty( $value ) ) 
  468. { 
  469. return ""; 
  470. } 
  471. $value = preg_replace( "/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i", "&111n2", $value ); 
  472. $value = preg_replace( "/<script(.*?)>(.*?)</script>/si", "", $value ); 
  473. $value = preg_replace( "/<iframe(.*?)>(.*?)</iframe>/si", "", $value ); 
  474. $value = preg_replace( "/<object.+</object>/iesU", "", $value ); 
  475. return $value; 
  476. } 
  477.  
  478. public static function filterHtml( $value ) 
  479. { 
  480. if ( emptyempty( $value ) ) 
  481. { 
  482. return ""; 
  483. } 
  484. if ( function_exists( "htmlspecialchars" ) ) 
  485. { 
  486. return htmlspecialchars( $value ); 
  487. } 
  488. return str_replace( array( "&", """, "'", "<", ">" ), array( "&amp;", "&quot;", "&#039;", "&lt;", "&gt;" ), $value ); 
  489. } 
  490.  
  491. public static function filterSql( $value ) 
  492. { 
  493. if ( emptyempty( $value ) ) 
  494. { 
  495. return ""; 
  496. } 
  497. $sql = array( "select", "insert", "update", "delete", "'", "/*", "../", "./", "union", "into", "load_file", "outfile" ); 
  498. $sql_re = array( "", "", "", "", "", "", "", "", "", "", "", "" ); 
  499. return str_ireplace( $sql, $sql_re, $value ); 
  500. } 
  501.  
  502. public static function filterStr( $value ) 
  503. { 
  504. if ( emptyempty( $value ) ) 
  505. { 
  506. return ""; 
  507. } 
  508. $value = trim( $value ); 
  509. $badstr = array( "x00", "%00", "r", "&", """, "'", "<", ">", "%3C", "%3E" ); 
  510. $newstr = array( "", "", "", "&amp;", "&quot;", "&#39;", "&lt;", "&gt;", "&lt;", "&gt;" ); 
  511. $value = str_ireplace( $badstr, $newstr, $value ); 
  512. $value = preg_replace( "/&amp;((#(d{3,5}|x[a-fA-F0-9]{4}));)/", "&1", $value ); 
  513. return $value; 
  514. } 
  515.  
  516. public static function filterUrl( ) 
  517. { 
  518. if ( preg_replace( "/https?://([^:/]+).*/i", "1", $_SERVER['HTTP_REFERER'] ) !== preg_replace( "/([^:]+).*/", "1", $_SERVER['HTTP_HOST'] ) ) 
  519. { //开源软件:Vevb.com 
  520. return FALSE; 
  521. } 
  522. return TRUE; 
  523. } 
  524.  
  525. public static function filterForbidChar( $content ) 
  526. { 
  527. $new_content = $content; 
  528. $forbidargs = X::$cfg['forbidargs']; 
  529. if ( !emptyempty( $forbidargs ) ) 
  530. { 
  531. $array = explode( ",", $forbidargs ); 
  532. $i = 0; 
  533. for ( ; $i < sizeof( $array );   ++$i    ) 
  534. { 
  535. $new_content = str_ireplace( $array[$i], "", $content ); 
  536. } 
  537. } 
  538. return $new_content; 
  539. } 
  540.  
  541. public static function checkExistsForbidChar( $content ) 
  542. { 
  543. $flag = FALSE; 
  544. $forbidargs = X::$cfg['forbidargs']; 
  545. if ( !emptyempty( $forbidargs ) ) 
  546. { 
  547. $array = explode( ",", $forbidargs ); 
  548. $i = 0; 
  549. for ( ; $i < sizeof( $array );   ++$i    ) 
  550. { 
  551. if ( FALSE === strpos( strtolower( $content ), strtolower( $array[$i] ) ) ) 
  552. { 
  553. continue; 
  554. } 
  555. $flag = TRUE; 
  556. break; 
  557. } 
  558. } 
  559. return $flag; 
  560. } 
  561.  
  562. public static function checkExistsForbidUserName( $username ) 
  563. { 
  564. $flag = FALSE; 
  565. $forbidargs = X::$cfg['lockusers']; 
  566. if ( !emptyempty( $forbidargs ) ) 
  567. { 
  568. $array = explode( ",", $forbidargs ); 
  569. $i = 0; 
  570. for ( ; $i < sizeof( $array );   ++$i    ) 
  571. { 
  572. if ( FALSE === strpos( strtolower( $username ), strtolower( $array[$i] ) ) ) 
  573. { 
  574. continue; 
  575. } 
  576. $flag = TRUE; 
  577. break; 
  578. } 
  579. } 
  580. return $flag; 
  581. } 
  582.  
  583. } 
  584.  
  585. if ( !defined( "IN_OESOFT" ) ) 
  586. { 
  587. exit( "Access Denied" ); 
  588. } 
  589. ?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表