Share a few PHP function functions that are customized

Source: Internet
Author: User
This article mainly introduces the sharing of custom PHP function functions, this article is listed in the Web development of a number of commonly used functions, such as submission filtering, string interception, IP address anonymity, hidden file true path, etc., the need for friends can refer to the next

Recently not toss the forum, various kinds of functions, original some, from others there Qiang come over some, here to share out, hope to have friends can use to ~

Note: Some functions may not be perfect, resulting in a risk of vulnerability

Submit Filter

function Filter ($text) {//full filter Comment $text = preg_replace ('/<!--?. *-->/', ', $text); Full filter js $text = preg_replace ('/<script?. *\/script>/', ', $text); Filter for dangerous properties 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 ('/(<[^><]+) (win dow\.| javascript:|js:|about:|file:|document\.| Vbs:|cookie) ([^><]*)/I ', $text, $mat)) {$text = Str_replace ($mat [0], $mat [1]. $mat [3], $text);}//filter extra HTML $text = Preg_replace ('/<\/? ( html|head|meta|link|base|basefont|body|bgsound|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name| script|xml) [^><]*>/i ', ', $text); Inverse 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 interception (This is the emlog, in fact, SUBSTR's enhanced version)

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 ($st Rings, ' UTF8 ')? $sub _str. ' ... ': $sub _str; } $str = substr ($strings, $start, $length); $char = 0; for ($i = 0; $i < strlen ($STR); $i + +) {if (Ord ($str [$i]) >=) $char + +,} $str 2 = substr ($strings, $start, $lengt H + 1); $str 3 = substr ($strings, $start, $length + 2); if ($char% 3 = = 1) {if ($length <= strlen ($strings)) {$STR 3 = $str 3. = ' ... ';} return $str 3;} if ($char% 3 = 2) {if ($length <= strlen ($strings)) {$str 2 = $str 2. = ' ... ';} return $str 2;} if ($char% 3 = = 0) {if ($length <= strlen ($strings)) {$str = $str. = ' ... ';} return $str;}}

IP address anonymity (last digit replaced by asterisk)

function Anonymousip () {$ip = GetIP (), if ($ip = = "Unknown") {$removed _ip = "Unknown user",} if (Strpos ($ip, ":")) {$removed _i p = "IPv6 user"; } else {$reg 1 = '/(?: \ D+\.) {3}) \d+/'; $reg 2 = ' ~ (\d+) \. (\d+) \. (\d+) \. (\d+) ~ '; $removed _ip = preg_replace ($reg 1, "\\1*", $IP); } return $removed _ip;}

Get 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); $dynKeyPart 1 = substr ($dynKey, 0, 20); $dynKeyPart 2 = substr ($dynKey, 20); $fixedKeyPart 1 = substr ($fixedKey, 0, 20); $fixedKeyPart 2 = substr ($fixedKey, 20);  $key = hash (' SHA1 ', $dynKeyPart 1. $fixedKeyPart 1. $dynKeyPart 2. $fixedKeyPart 2); $string = $isEncrypt? $fixedKeyPart 1. $string. $dynKeyPart 2: (Isset ($string {339})? Gzuncompress (Base64_decode (substr ($string,)): 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%));} return $isEncrypt? $dynKey. Str_replace (' = ', ' ', Base64_encode ($n > 299? gzcompress ($result): $result)): substr ($result, -20);} 

Check if string contains Chinese

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

Hide file True 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.