This article is mainly to share with you some PHP tool functions introduced, these are more commonly used tool functions, hope to help everyone.
1. Get millisecond-level timestamps
public static function Get_millisecond () { //Gets the timestamp of milliseconds $time = Explode ("", Microtime ()); $time = $time [1]. substr ($time [0], 2, 3); return $time; }
2 Read File contents
public static function File_read ($file _path) { if (file_exists ($file _path)) { $fp = fopen ($file _path, "R" ); $str = Fread ($fp, FileSize ($file _path));//Specify the read size, where the entire contents of the file are read $str = Str_replace ("\ r \ n", "<br/>", $str); C13/>fclose ($FP); return $str; } else { return false; } }
3 Generate random string, not longer than 32 bits
public static function Get_nonce_number ($length = one) { $chars = "0123456789"; $str = ""; for ($i = 0; $i < $length; $i + +) { $str. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1); } return $str; }
4. Generate Order detail number by default to 16 bits
public static function get_order_id ($length = +) { //See method one for millisecond-level timestamp $time = Self::get_millisecond (); c13/> $len = $length -13; $str = Self::get_nonce_number ($len); if (strlen ($time)! =) { $orderId = $str. $time. Rand (1, 9); } else { $orderId = $str. $time; } return $orderId; }
5. Random generation of letters and numbers (with the possibility of repetition)
public static function Randomkeys ($length) { $returnStr = '; $pattern = ' 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ '; for ($i = 0; $i < $length; $i + +) { $returnStr. = $pattern {Mt_rand (0, 61)};//Generate PHP random number } return $returnStr; c8/>}
6. Generate 6-digit verification Code
public static function Random_code ($length = 6) { $code = rand (POW (1, ($length-1)), POW ($length)-);
return "$code"; }
7. Anti-SQL injection, XSS attack
public static function Clean ($STR) { $str = trim ($STR); $str = Strip_tags ($STR); $str = Stripslashes ($STR); $str = Addslashes ($STR); $str = Rawurldecode ($STR); $str = Quotemeta ($STR); $str = Htmlspecialchars ($STR); return $str; }
8. Receive post or get parameters
public static function Get_input ($name) { $request = Yii:: $app->request; if (Isset ($_get[$name])) { $value = $request->get ($name), } elseif (Isset ($_post[$name])) { $value = $ Request->post ($name); } else { $value = '; } return $value; }
9. Encryption method
public static function Aes_encrypt ($STR) { $privateKey = "Private key file string"; $iv = "Public key file string"; Encryption $encrypted = Mcrypt_encrypt (mcrypt_rijndael_128, $privateKey, $str, MCRYPT_MODE_CBC, $IV);// return UrlEncode (Base64_encode ($encrypted)); Return Base64_encode ($encrypted); }
10. Decryption method
public static function Aes_decrypt ($STR) { $privateKey = "Private key file string"; $iv = "Public key file string"; $encryptedData = Base64_decode ($STR); $decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv); $decrypted = RTrim ($decrypted, "n"); return $decrypted; }
11.curl simulation of Get requests with header information
public static function Curl_get ($request _url, $session =[]) { if ($session) { $ch = Curl_init (); $header = ["Session:". $session]; Set up a header for your browser agent curl_setopt ($ch, Curlopt_httpheader, $header); curl_setopt ($ch, Curlopt_header, 0); Return response header information curl_setopt ($ch, Curlopt_url, $request _url); curl_setopt ($ch, Curlopt_returntransfer, 1);//Do not return data $data _json=curl_exec ($ch) on the page output; Curl_close ($ch); } else{ $curl = new Curl (); $data _json = $curl->get ($request _url); } $data = Json_decode ($data _json, true); return $data; }
12.curl Post method for simulating header information
public static function Curl_post ($request _url, $params, $session = "") {if ($session) {$curl = Curl_init ( ); Initialize $header = ["Session:". $session]; Set up a header for your browser agent curl_setopt ($curl, Curlopt_url, $request _url); Set url//curl_setopt ($curl, Curlopt_httpauth,curlauth_basic); Set HTTP authentication method curl_setopt ($curl, Curlopt_httpheader, $header); Set Header information curl_setopt ($curl, curlopt_returntransfer,1); Sets the return mode of the information obtained by Curl_exec curl_setopt ($curl, curlopt_post,1); Set send mode for POST request curl_setopt ($curl, Curlopt_postfields, $params); Set post Data curl_setopt ($curl, Curlopt_returntransfer, 1);//Do not return data from the page output $data _json = curl_exec ($cur L); if ($data _json = = = False) {echo Curl_errno ($curl); Exit (); } curl_close ($curl); }else{$curl = new curl (); $data _json = $curl->setoption (curlopt_postfields, Http_builD_query ($params))->post ($request _url); } $data = Json_decode ($data _json, true); return $data; }
13. Remotely download images to local
public static function Downloadimage ($url, $user _token) { $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout,); curl_setopt ($ch, Curlopt_ssl_verifypeer, false); $file = curl_exec ($ch); Curl_close ($ch); $filename = $user _token. ". JPG "; $path = "./images/user/". $filename; Self::saveasimage ($file, $path); } Private static function Saveasimage ($file, $path) { $resource = fopen ($path, ' a '); Fwrite ($resource, $file); Fclose ($resource); }
14. Seven KN upload image
public static function actionimg ($filename, $filepath) {$auth = new Au Th (yii:: $app->params["Qiniu_params" ["AccessKey"]), Trim (yii:: $app->params["Qiniu_params" ["Secretke Y "])); $UPLOADMGR = new UploadManager (); $token = $auth->uploadtoken (Yii:: $app->params["Qiniu_params" ["Upload_path"])); List ($SUCC, $fail) = $uploadMgr->putfile ($token, $filename, $filepath); $url = Yii:: $app->params["Qiniu_params" ["Picture_url"]. "/" . $SUCC ["Key"]; return $url; }