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() 计算数组中的元素数目或对象中 ...
随机推荐
- c/c++ 浮点型处理
#include <stdio.h> #include <iostream> #include <string> #include <string.h> ...
- netsh命令之ip设置
批处理.VBS实现自动设置IP.默认网关.DNS.WINS.IE代理的代码,需要的朋友可以参考下 因为公司有同事负责大连.沈阳两个城市,经常在两地来回走动,到每个城市后,都要自己手动更改相应的网络配置 ...
- cPanel 安装方法
. 如何安装cPanel/WHM? 答:安装过程较长,建议使用screen或nohupscreen: yum -y install screen perl screen -S cpanel cd /h ...
- Spring Bean 生命周期
转自:也谈Spring Bean的生命周期 开篇先用一张老图描述下Spring中Bean容器的生命周期. 插叙一下,记得某个博文中提到:“Spring的Bean容器只管理非单例Bean的生命周期,单例 ...
- js 放大镜用法bug解决
<img id="zoom_02" src='img/zhang5.jpg' data-zoom-image="img/zhang5p.jpg" /> ...
- virtualenv 管理python 环境
virualenvvirtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能够:1. 在没有权限的情况下安装新套件2. 不同应用可以使用不同的套件版本3. 套件升级不 ...
- VS2012+EF6+Mysq
为了学习ORM,选择了EntityFramework,经历了三天两夜的煎熬,N多次错误,在群里高手的帮助下,终于成功,现在将我的心路历程记录下来,一是让自己有个记录,另外就是让其它人少走些弯路. 我的 ...
- VG、LV、rezise2fs、lvresize、fuer使用说明
南沙节点改变LV大小,参考鸟哥第 570页 1.# resize2fs /dev/mapper/vg_niotsvr3-lv_home 150G resize2fs 1.41.12 (17-May-2 ...
- .net简单的静态页生成
1.得到实体对象model,读取模板 string htmlMaster = File.ReadAllText(HttpContext.Current.Server.MapPath("/ma ...
- spring 基于XML和注解的两种事务配置方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...