yii随笔(三) 开启友好路径

概述:实现 localhost/site/login 访问 localhost/index.php?r=site/login
1. web server 配置
a. nginx 配置
location / {

try_files $uri $uri/ /index.php?$args;
}

b. apache 配置

DocumentRoot "webroot"


RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php

# ...other settings...

2. 在配置文件中的 components 里 添加如下(配置urlManager)
'urlManager' =>[
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
],

发表在 yii2 | yii随笔(三) 开启友好路径已关闭评论

yii 随笔(二):创建第一个用户(yii2脚本程序初识)

1. 进入到 webroot/advanced (yii安装目录)

2.创建数据库, 配置数据库
配置文件路径为:webroot/advanced/common/config/main-local.php

3. 进入到 webroot/advanced目录, 执行 yii migrate 命令(migrate命令是yii自带的数据库转移工具,可对数据库进行备份升级等操作)
该命令实质执行的数据文件是:webroot/advanced/console/migrations/*.php
该命令的执行控制器文件是:webroot/advanced/vendor/yiisoft/yii2/console/controllers/MigrateController.php

4. 现在有了现在数据库里有了 user表,现在建立一个可登陆的用户

5. 用命令行创建password
1). 创建新的命令行控制器

webroot/advanced/console/controllers/testController.php

2). 在新的命令行控制器写下

/**
 * 主命令
 * 本例预计执行示例:在yii根目录执行 yii test(或者 yii test/tes) 
 */

//不可少
namespace console\controllers;
//不可少
use Yii;
//控制器的名字首字母必须为大写,继承 console 控制器
class TestController extends \yii\console\Controller{
    //默认执行的子命令,首字母必须为小写
    public $defaultAction = 'tes';
    
    //执行的子命令
    public function actionTes(){
        echo Yii::$app->getSecurity()->generatePasswordHash('123123');//123123 是需要被设置的密码
        return 0;
    }
}

发表在 yii2 | yii 随笔(二):创建第一个用户(yii2脚本程序初识)已关闭评论

yii 随笔(一):初始化环境

1. 下载高级模板包(我选择的是解压安装)
http://www.yiiframework.com/download/

2. 解压安装包(我下载的是https://github.com/yiisoft/yii2/releases/download/2.0.6/yii-advanced-app-2.0.6.tgz),到 webroot/advanced

3. 进到 webroot/advanced 执行 init,跟着提示走

4. 开启apache(关于安装,配置略)

5.访问前台 http://localhost/advanced/frontend/web/

发表在 yii2 | yii 随笔(一):初始化环境已关闭评论

php 中 str_replace或者preg_replace对php代码的性能的影响

str_replace或者preg_replace对php代码的性能的影响,如下:


$str = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
$time = array_sum(explode(' ',microtime() ) );
for($i = 0;$i < 500; $i++){
	$str = $str;
}
echo 'time is: ' .(array_sum(explode(' ',microtime() ) ) - $time).'<br />';
$time = array_sum(explode(' ',microtime() ) );
for($i = 0;$i < 500; $i++){
	$str = str_replace('dolor','dolor',$str);
}
echo 'time is: ' .(array_sum(explode(' ',microtime() ) ) - $time).'(str_replace)<br />';
$time = array_sum(explode(' ',microtime() ) );
for($i = 0;$i < 500; $i++){
	$str = preg_replace("/dolor/",'dolor', $str);
}
echo 'time is: ' .(array_sum(explode(' ',microtime() ) ) - $time).'(preg_replace)<br />';
exit;
//time is: 0.000119924545288
//time is: 0.0014500617981(str_replace)
//time is: 0.00306391716003(preg_replace)
发表在 php 优化 | php 中 str_replace或者preg_replace对php代码的性能的影响已关闭评论

mysql left join,right join,inner join 的区别

mysql> select * from str;
+----+-----------+-----------+
| id | string    | scenario  |
+----+-----------+-----------+
|  1 | dddd      |           | 
|  2 | ssssff    | ffff      | 
|  3 | 1231      | 23123123  | 
|  4 | 123123123 | 123123123 | 
|  5 | ssssff    | ffff      | 
|  6 | asdfas    | fffffdsdf | 
+----+-----------+-----------+
6 rows in set (0.00 sec)

mysql> select * from str1;
+------+------+
| s_id | tag  |
+------+------+
|    1 | 33   | 
|    4 | aas  | 
|    5 | asdf | 
|    7 | sdf  | 
|    8 | asdf | 
+------+------+
5 rows in set (0.00 sec)

mysql> select * from str s left join str1  t on s.id = t.s_id;
+----+-----------+-----------+------+------+
| id | string    | scenario  | s_id | tag  |
+----+-----------+-----------+------+------+
|  1 | dddd      |           |    1 | 33   | 
|  2 | ssssff    | ffff      | NULL | NULL | 
|  3 | 1231      | 23123123  | NULL | NULL | 
|  4 | 123123123 | 123123123 |    4 | aas  | 
|  5 | ssssff    | ffff      |    5 | asdf | 
|  6 | asdfas    | fffffdsdf | NULL | NULL | 
+----+-----------+-----------+------+------+
6 rows in set (0.00 sec)

mysql> select * from str s right join str1  t on s.id = t.s_id;
+------+-----------+-----------+------+------+
| id   | string    | scenario  | s_id | tag  |
+------+-----------+-----------+------+------+
|    1 | dddd      |           |    1 | 33   | 
|    4 | 123123123 | 123123123 |    4 | aas  | 
|    5 | ssssff    | ffff      |    5 | asdf | 
| NULL | NULL      | NULL      |    7 | sdf  | 
| NULL | NULL      | NULL      |    8 | asdf | 
+------+-----------+-----------+------+------+
5 rows in set (0.00 sec)

mysql> select * from str s inner join str1  t on s.id = t.s_id;
+----+-----------+-----------+------+------+
| id | string    | scenario  | s_id | tag  |
+----+-----------+-----------+------+------+
|  1 | dddd      |           |    1 | 33   | 
|  4 | 123123123 | 123123123 |    4 | aas  | 
|  5 | ssssff    | ffff      |    5 | asdf | 
+----+-----------+-----------+------+------+
3 rows in set (0.00 sec)

mysql> select * from str s inner join str1  t ;
+----+-----------+-----------+------+------+
| id | string    | scenario  | s_id | tag  |
+----+-----------+-----------+------+------+
|  1 | dddd      |           |    1 | 33   | 
|  1 | dddd      |           |    4 | aas  | 
|  1 | dddd      |           |    5 | asdf | 
|  1 | dddd      |           |    7 | sdf  | 
|  1 | dddd      |           |    8 | asdf | 
|  2 | ssssff    | ffff      |    1 | 33   | 
|  2 | ssssff    | ffff      |    4 | aas  | 
|  2 | ssssff    | ffff      |    5 | asdf | 
|  2 | ssssff    | ffff      |    7 | sdf  | 
|  2 | ssssff    | ffff      |    8 | asdf | 
|  3 | 1231      | 23123123  |    1 | 33   | 
|  3 | 1231      | 23123123  |    4 | aas  | 
|  3 | 1231      | 23123123  |    5 | asdf | 
|  3 | 1231      | 23123123  |    7 | sdf  | 
|  3 | 1231      | 23123123  |    8 | asdf | 
|  4 | 123123123 | 123123123 |    1 | 33   | 
|  4 | 123123123 | 123123123 |    4 | aas  | 
|  4 | 123123123 | 123123123 |    5 | asdf | 
|  4 | 123123123 | 123123123 |    7 | sdf  | 
|  4 | 123123123 | 123123123 |    8 | asdf | 
|  5 | ssssff    | ffff      |    1 | 33   | 
|  5 | ssssff    | ffff      |    4 | aas  | 
|  5 | ssssff    | ffff      |    5 | asdf | 
|  5 | ssssff    | ffff      |    7 | sdf  | 
|  5 | ssssff    | ffff      |    8 | asdf | 
|  6 | asdfas    | fffffdsdf |    1 | 33   | 
|  6 | asdfas    | fffffdsdf |    4 | aas  | 
|  6 | asdfas    | fffffdsdf |    5 | asdf | 
|  6 | asdfas    | fffffdsdf |    7 | sdf  | 
|  6 | asdfas    | fffffdsdf |    8 | asdf | 
+----+-----------+-----------+------+------+
30 rows in set (0.00 sec)
发表在 mysql | mysql left join,right join,inner join 的区别已关闭评论

判断客户端是否是手机


function is_mobile(){
	$regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
	$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
	$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
	$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
	$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
	$regex_match.=")/i";
	
	return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}

转自:http://snipplr.com/view/48763/simple-user-agent-mobile-and-smartphone-detection-and-redirction-with-php/

发表在 php函数集 | 判断客户端是否是手机已关闭评论

php计算月份差

/*
 *取某个日期到现在时间的月份差值。如果不够一个月,按一个月算
 */
function diffMonth($date){
        if(strtotime($date) < time() ){
                $start = $date;
                $end  = date('Y-n-j');
        }else{
                $start = date('Y-n-j');
                $end  = $date;
        }
        $starY = date("Y",strtotime($start));
        $starM = date("n",strtotime($start));
        $starD = date("j",strtotime($start));
	
        $nowY = date("Y",strtotime($end));
        $nowM = date("n",strtotime($end));
        $nowD = date("j",strtotime($end));
	
        $diffM = 0;
	
        if($starY == $nowY){
                if($starM == $nowM){
                        if($starD < $nowD){
                                $diffM = 1;
                        }elseif($starD = $nowD){
                                $diffM = 0;
                        }else{
                                $diffM = false;
                        }
                }elseif($starM < $nowM){
                        if($starD < $nowD){
                                $diffM = $nowM - $starM + 1;
                        }else{
                                $diffM = $nowM - $starM;
                        }
                }else{
                        $diffM = false;
                }
        }elseif($starY < $nowY){
                $diffY = $nowY - $starY;
                if($starD < $nowD){
                        $diffM = (12 - $starM + $nowM + 1) + 12 * ($diffY - 1);
                }else{
                        $diffM = (12 - $starM + $nowM) + 12 * ($diffY - 1);
                }
	
        }else{
                $diffM = false;
        }
	
        return $diffM;
}
发表在 php函数集 | 一条评论

js 得到函数名称

function getFunctionName(fn) {
	if (typeof fn != "function") throw "the argument must be a function.";
	var reg = /W*functions+([w$]+)s*\(/;
	var name = reg.exec(fn);
	if (!name) {
	return '(Anonymous)';
	}
	return name[1];
}
发表在 js | js 得到函数名称已关闭评论

js 得到dom对象的类型

function getType(x) {
	//如果x为null,则返回null
	if (x == null) return "null";
	var t = typeof x;
	//如果x为简单类型,则返回类型名称
	if (t.toLocaleLowerCase() != "object") return t;
	//调用object类的toString方法得到类型信息
	//object.toString方法返回类似这样的信息[object 类名]
	t = Object.prototype.toString.apply(x).toLowerCase();
	//截取toString方法返回值的类名部分
	t = t.substring(8, t.length - 1);
	if (t.toLocaleLowerCase() != "object") return t;
	//检查x确实为object类型
	if (x.constructor == Object) return t;
	//从构造函数得到类型名称
	if (typeof x.constructor == "function")
	return getFunctionName(x.constructor);
	return "unknow type";
}
发表在 js | js 得到dom对象的类型已关闭评论

数猴子的小程序(约瑟夫环)

/*
 *一群猴子排成一圈,按1,2,...,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去...,如此不停的进行下去,
 *直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程最新的新浪PHP面试题模拟此过程,输入m、n, 输出最后那个大王的编号。 
 *
 *此函数的原理是 比如:array(1=>1,2=>2,3=>3); 要数每当2的时候出局,则数到2的时候,把"1=>1"这个值移到 "4=>1",即2=>2这个值出局后的数组为array(3=>3,4=>1);
 */
function fundLastMonk($n,$m){
	$arr = range(0,$n);//生成数组
	unset($arr[0]);//从序号1开始的数组
	$i = 0;
	while(1){
		$i++;				
		if($i % $m == 0){
			if($arr[$i] == null){//当被删除的值为空时,则不停的把数组的第一个值移到最后面,直到该值不为空。(这种情况可能是因为:数组的值的数量少于出局数,或者数组的值不够,把已经数过的值移到后面)
				while(1){
					foreach($arr as $key=>$val){
						if($arr[$i] != null) break 2;
						$arr[] = $val;
						unset($arr[$key]);
					}
				}
				
				if($arr[$i + 1] == null){//当要被出局的值是数组的最后一个值的时候,先把前面的值移到后面
					foreach($arr as $key=>$val){
						if($key == $i) break;
						$arr[] = $val;
						unset($arr[$key]);
					}
				}
			}else{//把数过的数移到数组的最后面
				foreach($arr as $key => $val){
					if($key == $i)break;
					$arr[] = $val;
					unset($arr[$key]);
				}
			}
			
			unset($arr[$i]);	
		}
		if(count($arr) == 1) {
			return array_shift($arr);
		}
	}
}

echo fundLastMonk(5,6);
发表在 php小程序 | 数猴子的小程序(约瑟夫环)已关闭评论