欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

常用自定义函数

程序员文章站 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 = '
  • 首页
  • '; } if($pageNum 尾页'; } //上一页 下一页 $upRow = $pageNum - 1; $downRow = $pageNum + 1; $upPage = $downPage = ''; if ($upRow > 0) { $upPage='
  • 上一页
  • '; } if ($downRow 下一页'; } //计算起始、结束页码 $start = max(1, $pageNum - intval($linksize/2)); $end = min($start + $linksize - 1, $totalPage); $start = max(1, $end - $linksize + 1); $pageStr = ''; for($i = $start; $i '.$i.' '; } else { $pageStr .= '
  • '.$i.'
  • '; } } $pageStr = $firstPage . $upPage . $pageStr . $downPage . $endPage; $pageStr .= '
  • 共'.$totalPage.'页'; return $pageStr; } //编码转汉字 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: 0xxxxxxx if($unicode_c_val = 0x80 && $unicode_c_val > 6 | $h; $c2 = ($unicode_c_val & 0x3F) | $f; $str = chr($c1).chr($c2); } //U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx else if($unicode_c_val >= 0x800 && $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 10xxxxxx 10xxxxxx 10xxxxxx else if($unicode_c_val >= 0x10000 && $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 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx else if($unicode_c_val >= 0x200000 && $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 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx else if($unicode_c_val >= 0x4000000 && $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; }