https://static.iklfy.com/static/images/index/6.jpg

PHP多种情况下生成随机字符串

/**

 * 生成随机字符串

 * @param  integer $length 长度

 * @param  boolean $int    是否纯数字

 * @param  integer $level  字符串强度[1-4]

 * @return [type]          [description]

 */

if (!function_exists('randoms')) {

    function randoms($length=6,$int=false,$level=2) {

        $character = [

            'number' => '0123456789',

            'letter' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',

            'symbol' => '!@#$%^&*()-_[]{}<>~+=/?|',

            'filter' => '0OIl1'

        ];

https://static.iklfy.com/static/images/index/16.jpg

php目录遍历

    /**
     * 遍历路径下文件存为数组
     * @param  String $path 要遍历文件的路径
     * @return Array  返回存入全局作用域中的数组
     */
    function traverse($path='.'){
        static $files_list = array();
        foreach ($files = array_diff(scandir($path),array('.','..')) as $file) {
            $filename = $path.'/'.$file;
            if (is_dir($filename)) {
                traverse($filename);
            }else{
                $files_list[] = $f                
https://static.iklfy.com/static/images/index/14.jpg

php转换字符串编码

    /**
     * 转换字符串编码
     * @param  String $string 要编码的字符串
     * @param  String $encoding 要转换成的编码类型 参考:mbstring模块支持的编码
     * @return String 编码后的字符串
     */
    function str_encoding_convert($string,$encoding='UTF-8'){
        $encoding_list = array('UTF-8','ASCII','GBK','GB2312','GB18030','BIG5');
        $string_encoding = mb_detect_encoding($string,'auto');
        $encoding = strtoupper($encoding);
        // 判断是否等同所设置编码
        if ($string_encoding === $encoding) {
            return $string;
  &nb                
https://static.iklfy.com/static/images/index/5.jpg

PHP语言的PclZip压缩和解压缩类标准方法。

<?php
include_once('include/pclzip.lib.php');
$archive = new PclZip('archive.zip');

//解压缩到extract/folder/这个目录中
$list = $archive->extract(PCLZIP_OPT_PATH, "extract/folder/");

//增加这个目录在压缩档中,完成以后压缩档里面会有backup这个目录,backup里面会有这两个档案
$list = $archive->create("file.txt,image.gif",PCLZIP_OPT_ADD_PATH, "backup");

//去掉部份的路径,这里完成后会变成test/file.txt
$list = $archive->add("/usr/local/user/test/file.txt",PCLZIP_OPT_REMOVE_PATH, "/usr/local/user");

//把所有路径都去掉,这个压缩档建立完后,里面就只会有file.txt跟image.gif,不会有目录了
$list = $archive->create("data/file.txt images/image.gif",PCLZIP_OPT_REMOVE_ALL_PATH);

//把解压缩出来的档案的CHMOD设成0777
$list = $archive->extract(PCLZIP_OPT_SET_CHMOD, 0777);

//解压缩部份的档案,这个参数是使用档案名称判别
//引数可以用下面这样的阵列
$rule_list[0] = 'test/aaa.txt';
$rule_list[1] = 'test/d                
https://static.iklfy.com/static/images/index/19.jpg

PHP创建或者生成GUID

什么是 GUID?

全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装。在许多流行软件应用程序(例如 Web 浏览器和媒体播放器)中,都使用 GUID。

GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。例如:{D19F0B53-D538-F56A-C70E-675DF2A804B7} 即为有效的 GUID 值。

为什么要用GUID?

世界上的任何两台计算机都不会生成重复的 GUID 值。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符。在 Windows 平台上,GUID 应用非常广泛:注册表、类及接口标识、数据库、甚至自动生成的机器名、目录名等。

function create_guid(){
    if (function_exists('com_create_guid')){
        return com_create_guid();
    }else{
        mt_srand((double)microtime()*10000);
        $unid = strtoupper(md5(uniqid(rand(),true)));
        return '{'.substr($unid,0,8).'-'.substr($unid,8,4).'-'.substr($unid,12,4).'-'.substr($unid,16,4).'-'.substr($unid,20,12).'}';
    }
}

echo create_guid();

https://static.iklfy.com/static/images/index/5.jpg

PHP字符串查询函数

function strexists($haystack, $needle)
/**
* 这个看成函数重载也无妨,功能就是查找$haystack是不是在$needle中存在
* @para string $haystack
* @para string $needle
* @return boolean
*/

function strexists($haystack, $needle) {
       return !(strpos($haystack, $needle) === FALSE);
}
https://static.iklfy.com/static/images/index/1.jpg

PHP通过session id 实现session共享和登录验证

先说说,这个机制的用途吧,到现在为止战地知道这个机制有两个方面的用途:
首先,多服务器共享session问题,这个大家应该都能够理解的,当一个网站的用户量过大,就会使用服务器集群,例如专门有一个登录用的服务器。用户通过登录服务器登录之后,登录服务器保存了用户的登录信息session,而其他受访问的服务器,例如电影服务器没有这个session,那么我们就要通过一个session的唯一标识来共享这个session了——具体session的共享超出了本文的范围,请自行查阅资料。
第二个用途就是,验证同一用户的不同会话,这个比较难理解。这样说吧,一个用户并非通过浏览器来请求连接,而是通过socket或者其它方式来请求数据的时候,我们首先要对他进行用户登录验证,验证成功之后,就下发一个sessionid给他,然后他每次请求的时候就携带这个sessionid,我们通过这个sessionid来判断session是否已经存在,如果存在我们就认定用户已经登录……
对于第一个问题,我们可以把sessionid保存在数据库中得以实现,这个方法比较安全而且应用广泛,但是不是我们讨论的范围哦
第二个问题,其实已经很简单了,看一下代码
首先验证的时候产生一个sessionid;
https://static.iklfy.com/attach/2012/06/18/1328872193_5353f7f6.png

PHP版网站缓存加快打开速度!

说明:
1,在服务器缓存了压缩过的文件,再次访问减少再压缩时间,降低CPU占用率。
2,通过设置客户端文件缓存时间,降低再次请求次数,可降低85%以上。
3,图片因为已经是压缩格式,只是设置客户端缓存时间,不做压缩处理。
使用方法:
1,服务器必须支持gzip,Rewrite功能。
2,在.htacess文件的“RewriteBase /”下面一行添加下面的代码,见图
RewriteRule (.*.css$|.*.js$|.*.jpg$|.*.gif$|.*.png$) gzip.php?$1 [L>
3,上传gzip.php到根目录
4,在根目录建cache文件夹,保证可读写。
https://static.iklfy.com/attach/2012/06/18/1328871899_58277269.png

WordPress如何判断当前用户是否为管理员

很多都有一些这样的需求,比如给管理员评论加上特殊的标记,比如给管理员输出特殊的内容等等。
我就碰到了这样一个需要判断当前用户是否为管理员的需求。
我在网上搜索了好多网页,貌似清一色都是说用is_admin这个函数,其实这个函数只是用来判断是否显示控制板或管理栏,也就是说是否处于后台控制面板中。这样这个函数对于前台模板这样的需求是没有用了。
后来经过再次查找,其实应该是current_user_can这个函数,这个函数用来判断当前用户是否具有某级别的权限。
看如下图表
https://static.iklfy.com/static/images/index/1.jpg

今天在新年的最后一天,我将离开BOB,转向WordPress

昨晚花了不少的时间对这个程序转换到WordPress程序,刚好今天是除夕夜!
其实并不是BOBLOG不好,而且更新太慢,有点跟不上社会的脚步,
之前我并不太看好WP,但是在辗转了N年之后,没想到又会重新回到WordPress的大家庭中来!
唉,发一段代码,也许大家有能用到,此代码的作用是将boblog2.1.1版本中的UBB代码全部替换为WordPress3.3.1中的标签!!
其他的部分我就不发了,相信用到的人也不会太多!这里发布一下我新站的地址,
我的新站将会改用 www.iklfy.com (备案中,暂时不启用) 而本站域名将不变!
www.iklfy.com BOBLOG博客
www.iklfy.com WordPress博客
欢迎大家前往我的新博客,目前数据已经完整迁移过去了!
https://static.iklfy.com/static/images/index/23.jpg

直接用$获取值而不$_GET

<?
function my_addslashes($string, $force = 0) {
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
if(!MAGIC_QUOTES_GPC || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = my_addslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
foreach(array('_COOKIE', '_POST', '_GET') as $_request) {
foreach($$_request as $_key => $_value) {
$_key{0} != '_' && $$_key = my_addslashes($_value);
}
}
echo $urls;
?>
https://static.iklfy.com/static/images/index/11.jpg

调用腾讯的API接口获取你所在当前城市名称

<?php   
/*
调用腾讯的API接口
返回结果 var IPData = new Array("114.238.55.147","","江苏省","淮安市");
0 为 IP地址
1 为 null
2 为 省份
3 为 城市
*/
function get_ip_place(){
$ip=file_get_contents("http://fw.qq.com/ipaddress");
$ip=str_replace('"',' ',$ip);
$ip2=explode("(",$ip);
$a=substr($ip2[1],0,-2);
$b=explode(",",$a);
return $b;
}
$ip=get_ip_place();
print_r($ip);
print_r($ip[0]); // 这个就是你当前外网IP地址
print_r($ip[2]); // 这个就是你所在的省份
print_r($ip[3]); //这个就是你所在的城市了
?>
https://static.iklfy.com/attach/2012/06/18/1316613632_7522dd13.png

PHPWind 8.5和8.7版的伪静态.htaccess规则

日前PHPWind发布了8.7的新版本,站长们升级后有些伪静态规则不能使用。本文介绍修改Apache的.htaccess来实现伪静态的效果,效果如下:

一、修改根目录下.htaccess规则,代码如下:
RewriteEngine on
RewriteRule f(\d+)_(.*)_(.*)$ /thread.php?fid=$1&page=$2&type=$3 [L]
RewriteRule f(\d+)_(.*)$ /thread.php?fid=$1&page=$2 [L]
RewriteRule f(\d+)$ /thread.php?fid=$1&page=$2 [L]
RewriteRule (\d+)_(.*)_(.*)_(.*).html$ /read.php?tid=$1&page=$2&fpage=$3&uid=$4 [L]
RewriteRule (\d+)_(.*)_(.*).html$ /read.php?tid=$1&page=$2&fpage=$3 [L]
RewriteRule (\d+)_(.*).html$ /read.php?tid=$1&page=$2 [L]
RewriteRule (\d+).html /read.php?tid=$1 [L]
RewriteRule ^(.*)-htm-(.*)$ $1.php?$2 [L]
Re
https://static.iklfy.com/static/images/index/3.jpg

PHP缩略图类

<?php
/**
* @author www.iklfy.com
* @copyright 2011
* @filename thumb.class.php
*/
$arrFile = array('p1.jpg', 'p2.gif', 'p4.txt', 'p3.png');
/**
* 得到等比例缩放的长宽
*/
function getNewSize($maxWidth, $maxHeight, $srcWidth, $srcHeight)
{
if ($srcWidth < $srcHeight)
{
$maxWidth = ($maxHeight / $srcHeight) * $srcWidth;
}
else
{
$maxHeight = ($maxWidth / $srcWidth) * $srcHeight;
}
return array('width' => $maxWidth, 'height' => $maxHeight);
}