Common User-Defined Functions

Source: Internet
Author: User
Common functions of encoding, such as getting an IP address, sending an email, curl encapsulation, encryption, decryption, String object, and conversion to an array, will be followed by a supplement without obtaining ipfunded IP () {if (getenv (X-Forwarded-) strcasecmp (getenv (X-Forwarded-For), unknown) {$ ipgetenv (X-Forwarded-For);} elseif (getenv (HTTP_CLIENT _

Some common functions of encoding, such as getting an IP address, sending an email, curl encapsulation, encryption, decryption, String object, and conversion to an array, will be supplemented. // get IPfunction ip address () {if (getenv ('x-Forwarded-For ') strcasecmp (getenv ('x-Forwarded-For'), 'unknown ')) {$ ip = getenv ('x-Forwarded-For ');} elseif (getenv ('HTTP _ CLIENT _

Some common functions of encoding, such as getting an IP address, sending an email, curl encapsulation, encryption, decryption, and decryption. <无>
// Obtain IPfunction ip () {if (getenv ('x-Forwarded-For ') & strcasecmp (getenv ('x-Forwarded-For'), 'unknown ')) {$ ip = getenv ('x-Forwarded-For ');} elseif (getenv ('HTTP _ CLIENT_IP') & strcasecmp (getenv ('HTTP _ CLIENT_IP '), 'unknon') {$ ip = getenv ('HTTP _ CLIENT_IP ');} elseif (getenv ('HTTP _ X_FORWARDED_FOR ') & strcasecmp (getenv ('HTTP _ X_FORWARDED_FOR '), 'unknown') {$ ip = getenv ('HTTP _ X_FORWARDED_FOR ');} elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'unknown ') {$ ip = getenv ('remote _ ADDR ');} elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], 'unknown ')) {$ ip = $ _ SERVER ['remote _ ADDR '];} return preg_match ("/[\ d \.] {7, 15}/", $ ip, $ matches )? $ Matches [0]: 'unknown ';} // curl encapsulate function curl ($ url, $ method = 'get', $ postFields = null, $ header = null) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt ($ ch, success, false); curl_setopt ($ ch, success, true ); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt ($ ch, CURLOPT_TIMEOUT, 5); if (strlen ($ url)> 5 & strtolower (sub Str ($ url, 0, 5) = "https") {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );} switch ($ method) {case 'post': curl_setopt ($ ch, CURLOPT_POST, true); if (! Empty ($ postFields) {if (is_array ($ postFields) | is_object ($ postFields) {if (is_object ($ postFields) $ postFields = Bin_Tools :: object2array ($ postFields); $ postBodyString = ""; $ postMultipart = false; foreach ($ postFields as $ k => $ v) {if ("@"! = Substr ($ v, 0, 1) {// determines whether a file is uploaded $ postBodyString. = "$ k = ". urlencode ($ v ). "&" ;}else {// multipart/form-data for file upload; otherwise, www-form-urlencoded $ postMultipart = true ;}} unset ($ k, $ v); if ($ postMultipart) {curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ postFields);} else {curl_setopt ($ ch, CURLOPT_POSTFIELDS, substr ($ postBodyString, 0, -1) ;}} else {curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ postFields) ;}} break; default: if (! Empty ($ postFields) & is_array ($ postFields) $ url. = (strpos ($ url ,'? ') = False? '? ':' & '). Http_build_query ($ postFields); break;} curl_setopt ($ ch, CURLOPT_URL, $ url); if (! Empty ($ header) & is_array ($ header) {curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header);} $ response = curl_exec ($ ch ); if (curl_errno ($ ch) {throw new Exception (curl_error ($ ch), 0) ;}curl_close ($ ch); return $ response ;} // function object_to_array ($ obj) {$ _ arr = is_object ($ obj )? Get_object_vars ($ obj): $ obj; foreach ($ _ arr as $ key => $ val) {$ val = (is_array ($ val) | is_object ($ val ))? Object_to_array ($ val): $ val; $ arr [$ key] = $ val;} return $ arr;} // send an email Based on phpmailerfunction sendmail ($ to, $ title, $ content) {$ mail = new lib_phpmail_phpmail (); $ mail-> CharSet = "UTF-8"; $ mail-> IsSMTP (); $ mail-> SMTPAuth = true; $ mail-> SMTPSecure = "ssl"; $ mail-> Host = "11.111.11.11: 465"; $ mail-> Port = 25; $ mail-> Username = "aaa@aaa.com "; $ mail-> Password = "123abc"; $ mail-> SetFrom ('aaa @ aaa.com ', 'info'); $ mail-> Add Address ($ to); $ mail-> IsHTML (true); $ mail-> Subject = $ title; $ mail-> Body = $ content; return $ mail-> Send ();} // function encrypt ($ encrypt, $ key = "key") {$ iv = mcrypt_create_iv (mcrypt_get_iv_size (encrypt, MCRYPT_MODE_ECB ), MCRYPT_RAND); $ passcrypt = mcrypt_encrypt (encrypt, $ key, $ encrypt, encrypt, $ iv); $ encode = base64_encode ($ passcrypt); return $ encode;} // decrypt the string Function decrypt ($ decrypt, $ key = "key") {$ decoded = base64_decode ($ decrypt); $ iv = mcrypt_create_iv (encrypt (decrypt, MCRYPT_MODE_ECB), MCRYPT_RAND ); $ decrypted = mcrypt_decrypt (encrypt, $ key, $ decoded, MCRYPT_MODE_ECB, $ iv); return $ decrypted;} // paging function getPage ($ count, $ pageNum, $ pagesize, $ linksize = 10) {if ($ count = 0) return ''; $ totalPage = ceil ($ Count/$ pagesize); $ p = 'page'; $ request_url = $ _ SERVER ['request _ URI ']; if (strpos ($ request_url ,'? ') {$ Url = $ request_url;} else {$ url = $ request_url .'? ';}$ Parse = parse_url ($ url); if (isset ($ parse ['query']) {parse_str ($ parse ['query'], $ params ); unset ($ params [$ p]); $ url = $ parse ['path']. '? '. Http_build_query ($ params);} // if ($ pageNum> 1) {$ firstPage ='
  • Homepage
  • ';} If ($ pageNum <$ totalPage) {$ endPage ='
  • Last page
  • ';} // Next page on the previous page $ upRow = $ pageNum-1; $ downRow = $ pageNum + 1; $ upPage = $ downPage = ''; if ($ upRow> 0) {$ upPage ='
  • Previous Page
  • ';} If ($ downRow <= $ totalPage) {$ downPage ='
  • Next Page
  • ';} // Start and end page number of the calculation. $ start = max (1, $ pageNum-intval ($ linksize/2 )); $ end = min ($ start + $ linksize-1, $ totalPage); $ start = max (1, $ end-$ linksize + 1); $ pageStr = ''; for ($ I = $ start; $ I <= $ end; $ I ++) {if ($ I = $ pageNum) {$ pageStr. ='
  • '. $ I .'
  • ';} Else {$ pageStr. ='
  • '. $ I .'
  • ';}}$ PageStr = $ firstPage. $ upPage. $ pageStr. $ downPage. $ endPage; $ pageStr. ='
  • Total '. $ totalPage. 'page'; return $ pageStr;} // encode the public function entities2utf8 ($ unicode_c) {$ unicode_c = preg_replace_callback ("/\&\#(. + ?) \;/", Function ($ unicode_str) {$ unicode_c_val = hexdec ($ unicode_str [1]); $ f = 0x80; // 10000000 $ str = ""; // U-00000000-U-0000007F: 0 xxxxxxx if ($ unicode_c_val <= 0x7F) {$ str = chr ($ unicode_c_val);} // U-00000080-U-000007FF: 110 xxxxx 10 xxxxxx else if ($ unicode_c_val> = 0x80 & $ unicode_c_val <= 0x7FF) {$ h = 0xC0; // 11000000 $ c1 = $ unicode_c_val> 6 | $ h; $ c2 = ($ unicode_c_val & 0x3F) | $ f; $ str = chr ($ c1 ). chr ($ c2);} // U-00000800-U-0000FFFF: 1110 xxxx 10 xxxxxx 10 xxxxxx else if ($ unicode_c_val> = 0x800 & $ unicode_c_val <= 0 xFFFF) {$ h = 0xE0; // 11100000 $ c1 = $ unicode_c_val> 12 | $ h; $ c2 = ($ unicode_c_val & 0xFC0)> 6) | $ f; $ c3 = ($ unicode_c_val & 0x3F) | $ f; $ str = chr ($ c1 ). chr ($ c2 ). chr ($ c3);} // U-00010000-U-001FFFFF: 11110xxx 10 xxxxxx 10 xxxxxx 10 xxxxxx else if ($ unicode_c_val> = 0x10000 & $ unicode_c_val <= 0x1FFFFF) {$ h = 0xF0; // 11110000 $ c1 = $ unicode_c_val> 18 | $ h; $ c2 = ($ unicode_c_val & 0x3F000)> 12) | $ f; $ c3 = ($ unicode_c_val & 0xFC0)> 6) | $ f; $ c4 = ($ unicode_c_val & 0x3F) | $ f; $ str = chr ($ c1 ). chr ($ c2 ). chr ($ c3 ). chr ($ c4);} // U-00200000-U-03FFFFFF: 111110xx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx else if ($ unicode_c_val> = 0x200000 & $ unicode_c_val <= 0x3FFFFFF) {$ h = 0xF8; // 11111000 $ c1 = $ unicode_c_val> 24 | $ h; $ c2 = ($ unicode_c_val & 0xFC0000)> 18) | $ f; $ c3 = ($ unicode_c_val & 0x3F000)> 12) | $ f; $ c4 = ($ unicode_c_val & 0xFC0)> 6) | $ f; $ c5 = ($ unicode_c_val & 0x3F) | $ f; $ str = chr ($ c1 ). chr ($ c2 ). chr ($ c3 ). chr ($ c4 ). chr ($ c5);} // U-04000000-U-7FFFFFFF: 1111110x 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx 10 xxxxxx else if ($ unicode_c_val> = 0x4000000 & $ unicode_c_val <= 0x7FFFFFFF) {$ h = 0xFC; // 11111100 $ c1 = $ unicode_c_val> 30 | $ h; $ c2 = ($ unicode_c_val & 0x3F000000)> 24) | $ f; $ c3 = ($ unicode_c_val & 0xFC0000)> 18) | $ f; $ c4 = ($ unicode_c_val & 0x3F000)> 12) | $ f; $ c5 = ($ unicode_c_val & 0xFC0)> 6) | $ f; $ c6 = ($ unicode_c_val & 0x3F) | $ f; $ str = chr ($ c1 ). chr ($ c2 ). chr ($ c3 ). chr ($ c4 ). chr ($ c5 ). chr ($ c6) ;}return $ str ;}, $ unicode_c); return $ unicode_c ;}
  • 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.