博客调整注册用户的权限为投稿者,欢迎大家可以踊跃的为PHP iask投稿提问,战地会不遗余力的作出尽量完美的解答。争取把PHPiask.com做成一个受众多人欢迎的PHP站点,希望能够帮助更多的PHP爱好者。
基于jquery的图片懒加载:LazyLoad插件参数详解
2011年9月14日
mysql随机查询数据
2011年5月7日
PHP 获得用户操作系统的换行符
2011年4月27日
/** * 获得用户操作系统的换行符 * * @access public * @return string */ function get_crlf(){ if (stristr($_SERVER['HTTP_USER_AGENT'], 'Win')){ $the_crlf = '\r\n'; } elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Mac')){ $the_crlf = '\r'; // for old MAC OS } else{ $the_crlf = '\n'; } return $the_crlf; }
PHP 获得用户的真实IP地址
2011年4月27日
/** * 获得用户的真实IP地址 * * @return string */ function real_ip(){ static $realip = NULL; if ($realip !== NULL){ return $realip; } if (isset($_SERVER)){ if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */ foreach ($arr AS $ip){ $ip = trim($ip); if ($ip != 'unknown'){ $realip = $ip; break; } } } elseif (isset($_SERVER['HTTP_CLIENT_IP'])){ $realip = $_SERVER['HTTP_CLIENT_IP']; } else{ if (isset($_SERVER['REMOTE_ADDR'])){ $realip = $_SERVER['REMOTE_ADDR']; } else{ $realip = '0.0.0.0'; } } } else{ if (getenv('HTTP_X_FORWARDED_FOR')){ $realip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_CLIENT_IP')){ $realip = getenv('HTTP_CLIENT_IP'); } else{ $realip = getenv('REMOTE_ADDR'); } } preg_match("/[\d\.]{7,15}/", $realip, $onlineip); $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0'; return $realip; }
PHP对象转成数组的函数
2011年4月26日
/** * 对象转数组 * @author 佚名 * @param object $obj * @return array */ 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)) ? $this->object_to_array($val) : $val; $arr[$key] = $val; } return $arr; }
PHP循环实现编码互转函数
2011年4月25日
/** * 循环实现编码互转 * * @param string $param(字符串,对象,或者数组),$currCharset当前编码,$toCharset期望编码 * @return 参数类型 */ function zhandi_iconv($param,$currCharset,$toCharset){ if ($currCharset != $toCharset){ if (is_string($param)){ return iconv($currCharset, $toCharset, $param); } elseif (is_array($param)){ foreach ($param as $key => $value){ $param[$key] = zhandi_iconv($value); } return $param; } elseif (is_object($param)){ foreach ($param as $key => $value){ $param->$key = zhandi_iconv($value); } return $param; } else{ return $param; } } return $param; }
以当前周为基准计算周的开始时间和结束时间
2011年4月24日
/** * 以当前周为基准计算周的开始时间和结束时间,从星期一开始 * @param int $pre 负数表示前推几周,整数表示后推几周,缺省当前周 * @return array(周开始时间时间戳,周结束时间时间戳) */ function wFristAndLast($pre=0){ $wn=date("w"); $wf=date("Y-m-d 00:00:00",strtotime("-".($wn-1)." days")); $we=date("Y-m-d 23:59:59",strtotime("+".(7-$wn)." days")); if($pre<0){ $wf=strtotime("-".(abs($pre)*7)." days",strtotime($wf)); $we=strtotime("-".(abs($pre)*7)." days",strtotime($we)); } if($pre>0){ $wf=strtotime("+".($pre*7)." days",strtotime($wf)); $we=strtotime("+".($pre*7)." days",strtotime($we)); } return array("firstday"=>$wf,"lastday"=>$we); }
PHP获取指定月份的第一天开始和最后一天结束的时间戳函数
2011年4月23日
/** * 获取指定月份的第一天开始和最后一天结束的时间戳 * * @param int $y 年份 $m 月份 * @return array(本月开始时间,本月结束时间) */ function mFristAndLast($y="",$m=""){ if($y=="") $y=date("Y"); if($m=="") $m=date("m"); $m=sprintf("%02d",intval($m)); $y=str_pad(intval($y),4,"0",STR_PAD_RIGHT); $m>12||$m<1?$m=1:$m=$m; $firstday=strtotime($y.$m."01000000"); $firstdaystr=date("Y-m-01",$firstday); $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day"))); return array("firstday"=>$firstday,"lastday"=>$lastday); }
PHP生成验证码图片函数
2011年4月22日
/** * 生成验证码图片 * * @param String $word 验证码在session中的变量名称 */ function valiCode($word='randcode'){ Header("Content-type: image/gif"); $border = 0; //是否要边框 1要:0不要 $how = 4; //验证码位数 $w = $how*15; //图片宽度 $h = 18; //图片高度 $fontsize = 10; //字体大小 $alpha = "abcdefghijkmnpqrstuvwxyz"; //验证码内容1:字母 $number = "23456789"; //验证码内容2:数字 $randcode = ""; //验证码字符串初始化 srand((double)microtime()*1000000); //初始化随机数种子 $im = ImageCreate($w, $h); //创建验证图片 /* * 绘制基本框架 */ $bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色 ImageFill($im, 0, 0, $bgcolor); //填充背景色 if($border) { $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色 ImageRectangle($im, 0, 0, $w-1, $h-1, $black);//绘制边框 } /* * 逐位产生随机字符 */ for($i=0; $i<$how; $i++) { $alpha_or_number = mt_rand(0, 1); //字母还是数字 $str = $alpha_or_number ? $alpha : $number; $which = mt_rand(0, strlen($str)-1); //取哪个字符 $code = substr($str, $which, 1); //取字符 $j = !$i ? 4 : $j+15; //绘字符位置 $color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); //字符随即颜色 ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符 $randcode .= $code; //逐位加入验证码字符串 } /* * 如果需要添加干扰就将注释去掉 * * 以下for()循环为绘背景干扰线代码 */ /* + -------------------------------绘背景干扰线 开始-------------------------------------------- + */ for($i=0; $i<5; $i++)//绘背景干扰线 { $color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干扰线颜色 ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1); //干扰线 } /* + -------------------------------绘背景干扰线 结束-------------------------------------- + */ /* * 如果需要添加干扰就将注释去掉 * * 以下for()循环为绘背景干扰点代码 */ /* + --------------------------------绘背景干扰点 开始------------------------------------------ + */ for($i=0; $i<$how*40; $i++)//绘背景干扰点 { $color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干扰点颜色 ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2); //干扰点 } /* + --------------------------------绘背景干扰点 结束------------------------------------------ + */ //把验证码字符串写入session 方便提交登录信息时检验验证码是否正确 例如:$_POST['randcode'] = $_SESSION['randcode'] $_SESSION[$word] = $randcode; /*绘图结束*/ Imagegif($im); ImageDestroy($im); /*绘图结束*/ }
PHP获取服务器IP函数
2011年4月21日
/** * 获取服务器的ip * @access public * @return string **/ function real_server_ip(){ static $serverip = NULL; if ($serverip !== NULL){ return $serverip; } if (isset($_SERVER)){ if (isset($_SERVER['SERVER_ADDR'])){ $serverip = $_SERVER['SERVER_ADDR']; } else{ $serverip = '0.0.0.0'; } } else{ $serverip = getenv('SERVER_ADDR'); } return $serverip; }

最近评论