自己常用的几个function

1、当遇到会被修改,但修改次数非常少,而且内容不太多时,存入数据库其实没有必要。这时我会选择写入文件
function catch_write($catch,$arr){
if(!empty($arr)){
$str = "<?php\r\n/**\r\n * @author Stephen\r\n */\r\n";
$str .= "$".$catch." = array(\r\n";
foreach ($arr as $key => $value){
$str .= $this->A_catch($key,$value,1);
}
$str .= ");\r\n?>";
$file = fopen(APP . 'tmp/cache/data/'.$catch.'.php',"w");
fwrite($file,$str);
fclose($file);
return true;
}else{
return false;
}

}

function A_catch($key,$arr,$i){
if(!isset($i)){
$i = 1;
}
$str = "";
for($j=0;$j<$i;$j++){
$str .="\t";
}
$i++;
$str .= "'".$key."' =>";
if(is_array($arr)){
$str .= " array(\r\n";
foreach ($arr as $key => $value){
$str .=$this->A_catch($key,$value,$i);
}
for($j=0;$j<$i-1;$j++){
$str .="\t";
}
$str .= ")";
}else{
$str .= "'".$arr."'";
}
$str .= ",\r\n";
return $str;
}

读取时:
function catch_read($catch){
include APP . 'tmp/cache/data/'.$catch.'.php';
if(isset(${$catch})){
return ${$catch};
}else{
return false;
}
}

缺点:必须先创建好文件,不过即使是数据库,表也是要先建表.
2、取IP

function getOnlineIP() {
$cip = getenv ( 'HTTP_CLIENT_IP' );
$xip = getenv ( 'HTTP_X_FORWARDED_FOR' );
$rip = getenv ( 'REMOTE_ADDR' );
$srip = $_SERVER ['REMOTE_ADDR'];

if ($cip && strcasecmp ( $cip, 'unknown' )) {
$onlineip = $cip;
} elseif ($xip && strcasecmp ( $xip, 'unknown' )) {
$onlineip = $xip;
} elseif ($rip && strcasecmp ( $rip, 'unknown' )) {
$onlineip = $rip;
} elseif ($srip && strcasecmp ( $srip, 'unknown' )) {
$onlineip = $srip;
}

preg_match ( "/[\d\.]{7,15}/", $onlineip, $match );
$onlineip = $match [0] ? $match [0] : 'unknown';

return $onlineip;
}

3、在发送邮件,RSS等需要加上本站域名时,不须要改动代码

if (! defined ( 'WEB_HOST' )) {
define ( 'WEB_HOST',isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值