常用自定义函数
程序员文章站
2022-06-04 20:25:49
...
编码常用的一些函数如获取IP发送邮件curl封装加解密字符串对象转数组后面会有补充 无 //获取IPfunction ip(){if(getenv('X-Forwarded-For')strcasecmp(getenv('X-Forwarded-For'), 'unknown')){$ip = getenv('X-Forwarded-For');}elseif(getenv('HTTP_CLIENT_
编码常用的一些函数 如获取IP 发送邮件 curl封装 加解密字符串 对象转数组 后面会有补充
//获取IP function 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'), 'unknown')) { $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封装 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, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); if (strlen($url) > 5 && strtolower(substr($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)) { //判断是不是文件上传 $postBodyString .= "$k=" . urlencode($v) . "&"; } else { //文件上传用multipart/form-data,否则用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; } //发送邮件 基于phpmailer function 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->AddAddress($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 ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND ); $passcrypt = mcrypt_encrypt ( MCRYPT_RIJNDAEL_256, $key, $encrypt, MCRYPT_MODE_ECB, $iv ); $encode = base64_encode ( $passcrypt ); return $encode; } //解密字符串 function decrypt($decrypt,$key="key") { $decoded = base64_decode ( $decrypt ); $iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND ); $decrypted = mcrypt_decrypt ( MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv ); return $decrypted; } //分页 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 = '
上一篇: 深入探讨PHP缓存技术