诚实的小猴子

Hey man, just code it!!!


  • 首页

  • 归档

  • 标签

  • 关于
诚实的小猴子

composer 中国镜像

发表于 2016-11-21

打开命令行输入如下命令
composer config repo.packagist composer http://packagist.phpcomposer.com

然后输入如下命令查看配置信息
composer config -l|grep url

显示如下信息,配置成功
[repositories.packagist.url] http://packagist.phpcomposer.com

诚实的小猴子

让NodeJS应用以守护进程运行

发表于 2016-11-21

前些天发现可以用supervisor让NodeJS应用后台运行,于是记录到
[部署NodeJS Web应用] 这篇博文里面了。
今天发现用 [forever] 也能实现,方法如下:

1
2
3
4
5
6
7
8
#安装forever
npm install forever -g
#启动应用
forever start http.js
#启动以后可以这样查看,运行中的应用,如图1
forever list
#停止应用
forever stop http.js

图 1


一张图片

更多命令请查看https://github.com/foreverjs/forever

诚实的小猴子

JS中switch中的比较方式

发表于 2016-11-21

这里有一段代码:

1
2
3
4
5
6
7
8
var a = '1';
switch(a) {
case 1:
console.log(1);
break;
default :
console.log('default');
}

本来期望打印出的是1,结果打印出了default,why?
原来js的switch在比较的时候使用的是全等, ‘1’ === 1 显然返回了false

诚实的小猴子

php概率抽奖算法

发表于 2016-11-21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
/**
* bingo
*
* @param float $probability 概率 例如:0.01
*
* @return bool
*/
function bingo($probability)
{
$denominator = (int)(1/$probability);
$numerator = (int)($probability*$denominator);
$chance = rand(1, $denominator);
return $chance <= $numerator;
}

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$prizes = array(
array('id' => 1, 'name' => '小米手机', 'probability' => '0.009'),
array('id' => 2, 'name' => 'iPhone 6s', 'probability' => '0.0001'),
array('id' => 3, 'name' => '优惠券300', 'probability' => '0.002'),
array('id' => 4, 'name' => '16G优盘', 'probability' => '0.01'),
);
while(true) {
shuffle($prizes);
foreach($prizes as $prize) {
$result = false;
if ($this->bingo($prize['probability'])) {
$result = $prize;
break;
}
}
if($result) {
break;
}
}
print_r($result);

诚实的小猴子

IIS伪静态配置

发表于 2016-11-21

把所有非静态文件请求都交给index.php处理,规则如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="OrgPage" stopProcessing="true">
<match url="^(.*)$"/>
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(.*)$"/>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php/{R:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

对应Apache的规则:

1
2
3
4
5
6
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

1…5678
诚实的小猴子

诚实的小猴子

36 日志
31 标签
© 2019 诚实的小猴子
由 Hexo 强力驱动
主题 - NexT.Mist