PHP个人常用函数封装
function GetIP(){
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$cip = $_SERVER["HTTP_CLIENT_IP"];
}elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}elseif(!empty($_SERVER["REMOTE_ADDR"])){
$cip = $_SERVER["REMOTE_ADDR"];
}else{
$cip = "";
}
return $cip;
}
function HttpRequest($url, $type = 'get', $data = '',$timeout = 10,$header = array())
{$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if (strtoupper($type) == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_ENCODING , 'gzip');
$result['response'] = curl_exec($ch);
$result['status']=curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}
function SaveLog($content = '', $filename = 'others')
{
$rootDir = \Config::get('app.rootDir');
$logdir = $rootDir . '/app/storage/logs/';
if (!is_dir($logdir)) mkdir($logdir, 0777, true);
$filename = $filename.'_'.date('ymd');
$filename = $logdir . $filename . ".log";
$fp = fopen($filename, "a+");
$line = 1;
while (stream_get_line($fp, 8192, "\n")) {
$line++;
}
if ($line > 9999) {
file_put_contents($filename, '');
$line = 1;
}
$info = '<' . sprintf("%04d", $line) . '>' . date("Y-m-d H:i:s") . '<>';
$string = $info . str_replace("\n", "", str_replace("\r", "", $content)) . "\r\n";
file_put_contents($filename, $string, FILE_APPEND);
fclose($fp);
}
/**
* 获取或保存文件内容
* @param string $filedir 文件路径
* @param string $content 文件内容
* @return string
*/
function FileContent($filedir = '', $content = '')
{
if (empty($filedir)) return '';
if (empty($content)) {
if (file_exists($filedir)) {
$fp = fopen($filedir, "r");
$content = file_get_contents($filedir);
fclose($fp);
return $content;
} else {
return '';
}
} else {
$fps = fopen($filedir, "a");
file_put_contents($filedir, $content);
fclose($fps);
return true;
}
}
PHP个人常用函数封装的更多相关文章
- php文件操作(最后进行文件常用函数封装)
文件信息相关API $filename="./1-file.php"; //filetype($filename):获取文件的类型,返回的是文件的类型 echo '文件类型为:', ...
- PHP常用函数封装
//二分查找function bin_sch($array, $low, $high, $k){ if ($low <= $high) { $mid = intval(($l ...
- Javascript:常用函数封装
//cookie function setCookie(name, value, iDay) { if(iDay!==false) { var oDate=new Date(); oDate.setD ...
- javascript常用函数封装——运动、cookie、ajax、获取行内样式兼容写法、拖拽
运动.cookie.ajax.获取行内样式兼容写法.拖拽封装大合集. //url,data,type,timeout,success,error function ajax(options){ //- ...
- Python:常用函数封装
def is_chinese(uchar): """判断一个unicode是否是汉字""" if uchar >= u'\u4e00' ...
- shell常用函数封装-main.sh
#!/bin/bash #sunlight sp monitor system #created on 2018/01/07#by chao.dong#used by sp servers consi ...
- C#验证邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP类等常用函数封装
#region 验证邮箱验证邮箱 /**//// <summary> /// 验证邮箱 /// </summary> /// <param name="sour ...
- 我自己的Javascript 库,封装了一些常用函数 Kingwell.js
我自己的Javascript 库,封装了一些常用函数 Kingwell.js 博客分类: Javascript javascript 库javascript库 现在Javascript库海量,流行的 ...
- PHP其它常用函数;<<<面向对象(OPP)的三大特性:封装、继承、加态:>>> <----面试题 ;构造方法、析构方法,魔术方法、set、get方法;静态;抽象类;接口
PHP其它常用函数: 赋值:$r->name = "元素"; 取值: echo $r->name; count() 计算数组中的元素数目或对象中 ...
随机推荐
- 3. Shell 基本运算符
1. 概述 1.1 Shell 支持多种运算符,包括以下几种 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 1.2 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例 ...
- Python基础学习笔记---5.输入\输出 I\O文件操作目录
在很多时候,你会想要让你的程序与用户(可能是你自己)交互.你会从用户那里得到输入,然后打印一些结果.我们可以分别使用 raw_input 和 print 语句来完成这些功能.对于输出,你也可以使用多种 ...
- 在命令提示符下,怎么查看windows开启了哪些服务?
net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ...
- linux下制作u盘启动盘
格式化u盘为fat32 fdisk -l #查看U盘盘符,或者 df -T umount /dev/sdb #先卸载u盘 mkfs.vfat /dev/sdb #格式化为fat32模式,或者 mkfs ...
- GlusterFS常用命令小结
# /etc/init.d/glusterd start # /etc/init.d/glusterd stop # /etc/init.d/glusterd status 2. 开机自动 ...
- Bank Interest
Bank Interest Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...
- malloc without free, what happens?
It's per-process. Once your process exits, the allocated memory is returned to the OS for use by oth ...
- Sql Server异常捕获 try catch
declare @num int begin try end try begin catch select ERROR_LINE() as 错误行数, ERROR_MESSAGE() as 错误消息 ...
- 转: Windows下安装Oracle Database 12c Release 1(12.1.0.2.0) - Enterprise Edition
http://www.cnblogs.com/xqzt/p/4395053.html Windows下安装Oracle Database 12c Release 1(12.1.0.2.0) - Ent ...
- 1.1 python 安装(Windows)
1.浏览器输入URL http://www.python.org/download/ https://www.python.org/downloads/release/python-2712/ Win ...