Share several custom PHP function _ php instances

Source: Internet
Author: User
This article mainly introduces several custom PHP functions. This article lists some common functional functions in WEB development, for example, submit filtering, string interception, Anonymization of IP addresses, and hiding the real path of a file. For more information, see some of the functions of different types, which are original functions, I want to share some qiang from others and hope some friends can use it ~

Note: Some functions may be incomplete, and the resulting vulnerability risks are borne by them ~

Submit filter

Function filter ($ text) {// Fully filter comments $ text = preg_replace ('/
 /', '', $ Text); // completely filter js $ text = preg_replace ('/
 
  
/', '', $ Text); // filter dangerous attributes, such as: filter on event lang js while (preg_match ('/(<[^> <] +) (lang | action | background | codebase | dynsrc | lowsrc) [^> <] +/I ', $ text, $ mat )) {$ text = str_replace ($ mat [0], $ mat [1], $ text);} while (preg_match ('/(<[^> <] +) (window \. | javascript: | js: | about: | file: | document \. | vbs: | cookie) ([^> <] *)/I ', $ text, $ mat) {$ text = str_replace ($ mat [0], $ mat [1]. $ mat [3], $ text);} // filter redundant html $ text = preg_repla Ce ('/<\/? (Html | head | meta | link | base | basefont | body | bgsound | script | form | iframe | frame | frameset | applet | id | ilayer | layer | name | script | xml) [^> <] *>/I ', '', $ text); // reverse conversion $ text = str_replace (' [',' <', $ text ); $ text = str_replace (']', '>', $ text); $ text = str_replace ('|', '"', $ text); return $ text ;}
 

Submit filter 2

function stripslashes_array(&$array){ while (list($key, $var) = each($array)) { if ($key != 'argc' && $key != 'argv' && (strtoupper($key) != $key || '' . intval($key) == "$key")) { if (is_string($var)) { $array[$key] = stripslashes($var); } if (is_array($var)) { $array[$key] = stripslashes_array($var); } } } return $array;}

String truncation (this is proposed in emlog, which is actually the enhanced version of substr)

function subString($strings, $start, $length){ if (function_exists('mb_substr') && function_exists('mb_strlen')) { $sub_str = mb_substr($strings, $start, $length, 'utf8'); return mb_strlen($sub_str, 'utf8') < mb_strlen($strings, 'utf8') ? $sub_str . '...' : $sub_str; } $str = substr($strings, $start, $length); $char = 0; for ($i = 0; $i < strlen($str); $i++) { if (ord($str[$i]) >= 128) $char++; } $str2 = substr($strings, $start, $length + 1); $str3 = substr($strings, $start, $length + 2); if ($char % 3 == 1) { if ($length <= strlen($strings)) { $str3 = $str3 .= '...'; } return $str3; } if ($char % 3 == 2) { if ($length <= strlen($strings)) { $str2 = $str2 .= '...'; } return $str2; } if ($char % 3 == 0) { if ($length <= strlen($strings)) { $str = $str .= '...'; } return $str; }}

Anonymization of IP addresses (the last digit is replaced with an asterisk)

Function AnonymousIP () {$ ip = GetIP (); if ($ ip = "Unknown") {$ removed_ip = "Unknown user";} if (strpos ($ ip, ":") {$ removed_ip = "IPv6 user";} else {$ reg1 = '/((? : \ D + \.) {3}) \ d +/'; $ reg2 = '~ (\ D +) \. (\ d + )~ '; $ Removed_ip = preg_replace ($ reg1, "\ 1 *", $ ip) ;}return $ removed_ip ;}

Obtain the client IP address

function GetIP(){ if (@$_SERVER["HTTP_X_FORWARDED_FOR"]) $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; else if (@$_SERVER["HTTP_CLIENT_IP"]) $ip = $_SERVER["HTTP_CLIENT_IP"]; else if (@$_SERVER["REMOTE_ADDR"]) $ip = $_SERVER["REMOTE_ADDR"]; else if (@getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if (@getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP"); else if (@getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR"); else $ip = "Unknown"; return $ip;}

String encryption (Chinese supported)

function dencrypt($string, $isEncrypt = true, $key = "youdian"){ if (!isset($string{0}) || !isset($key{0})) { return false; }  $dynKey = $isEncrypt ? hash('sha1', microtime(true)) : substr($string, 0, 40); $fixedKey = hash('sha1', $key);  $dynKeyPart1 = substr($dynKey, 0, 20); $dynKeyPart2 = substr($dynKey, 20); $fixedKeyPart1 = substr($fixedKey, 0, 20); $fixedKeyPart2 = substr($fixedKey, 20); $key = hash('sha1', $dynKeyPart1 . $fixedKeyPart1 . $dynKeyPart2 . $fixedKeyPart2);  $string = $isEncrypt ? $fixedKeyPart1 . $string . $dynKeyPart2 : (isset($string{339}) ? gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40)));  $n = 0; $result = ''; $len = strlen($string);  for ($n = 0; $n < $len; $n++) { $result .= chr(ord($string{$n}) ^ ord($key{$n % 40})); } return $isEncrypt ? $dynKey . str_replace('=', '', base64_encode($n > 299 ? gzcompress($result) : $result)) : substr($result, 20, -20);}

Check whether the string contains Chinese characters

function CheckChinese($string){ if (preg_match("/[\x7f-\xff]/", $string)) { return true; } else { return false; }}

Hide the real file path

function FileHeaderJump($file){ header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit;}

Generate random string, custom length

function CreateRandomString($length){ $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $password = ""; for ($i = 0; $i < $length; $i++) { $password .= $chars[mt_rand(0, strlen($chars) - 1)]; } return $password;}

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.