« Posts under Web|PHP|MySQL|jQuery

PHP strtotime()的bug

因为strtotime()计算日期很方便,所以一直用它比较多,也知道会有些bug,但之前没遇到过。今天终于领教了。

PHP
//当前日期为2011-03-11
echo date('Y-m', strtotime("last month"));
//or
echo date('Y-m', strtotime("-1 month"));

返回结果是”2011-03″,想想应该是2月份28到的缘故吧。所以,结论就是strtotime()在某些情况下是不适用的。

Apache ALERT – configured POST variable limit exceeded

最近公司有用户反应系统表单的数据没有办法完整保存。测试后发现确实有问题,post提交表单后只能收到200个项目。查Apache日志发现如下记录:

[Fri Mar 11 11:34:13 2011] [error] [client 10.229.9.171]
ALERT - configured POST variable limit exceeded -
dropped variable 'test[]' (attacker '10.229.9.171', file '/web/www/Nerr/post.php'),
referer: http://10.229.8.16/Nerr/post.php

查了些资料:

http://blog.motane.lu/2009/09/15/alert-configured-post-variable-limit-exceeded/

http://blog.yam.com/ymwen/article/15330907

两篇文章分别都提到了[suhosin],像第一篇中提到的,我的apache conf中没有suhosin,咋办内?Just create it,重起apache,搞定。

[suhosin]
suhosin.request.max_vars = 1000
suhosin.post.max_vars = 1000

Nerrsoft的Tweets页面

好吧, 这是一个简易的Tweets页面, 应推友@lene_wf要求写了几个步骤, 实现的关键是你的WP主机要在国外, 或者或者….使用代理或是自建API之类的, 但这个不是讨论的重点.

页面的CSS copy了”中文推特圈”或是”rabr”, 额~~~~为什么是或? 说真的, 我的确不记得了~~~~~

1. 创建 一个WordPress Page, 取名为”Tweets”(当然,你可以根据需求命名)
2.下载Arc90的Full Twitter API client library written in PHP , 放在WP当前theme目录下面
3.修改WP当前theme的page.php, 在输出titel行下面(the_title();这行),加入如下代码:

PHP
< ?php 
if(the_title('','',false)=='Tweets'){
	include('twitter.php');
}
?>

记得那个’Tweets’就是step 1中的页面名称.

4.下载下面的twitter.txt, 修改后缀名为.php, 然后编辑该脚本. 找到$tUsername和$tPassword, 修改为你的Twitter用户名和密码, 保存

5.Test, 如果没有意外应该可以在你WP的相应页面看到你的推了

 

效果参见: http://nerrsoft.com/tweets

我突然想, 要不要把它做成Plugin? 但好像意义不大. 那就算了吧. HOH

»Read More

iframe中的PHP Session问题

今天的工作中遇到了一个问题, 用iframe将一个php脚本嵌入另一个页面, 提交脚本表单后Session出了问题, 似乎无法保存(Firefox或Chrome无此问题, 仅在IE环境下有问题).

使用session_id()查看后发现, 在IE下iframe内容更新后Session ID会随之发生变化, 也就是说原有的Session不再生效了, 当前的Session是全新并空的.

搜索Google后得到解决办法, 在PHP脚本输出内容之前加入一行header, 如下

PHP
1
2
3
4
5
6
<?php
header('P3P: CP="CAO PSA OUR"'); //ADD IN THIS LINE IN ORDER TO SOLVE THE INTERNET EXPLORER ALWAYS GET NEW SESSION ISSUE
session_start();
$_SESSION['test'] = 'anything';
echo $_SESSION['test'];
?>

配置Nginx代理Foursquare

自从Foursquare被墙之后一直用VPN上去checkin,虽说速度还不错,但每次都要先拨通VPN着实麻烦了点。

所以今天装了个支持API版的,试了下Google code上那个PHP版的代理,发现在nginx上要重写rewirte,索性在nginx上直接配个到api.foursquare.com的代理算了。

CONF
server {
    listen          0.0.0.0:80;
    server_name     foursquare.yourdomainname.com;
 
    #access_log      /var/log/nginx/4sq.access_log;
    #error_log       /var/log/nginx/4sq.error_log info;
 
    location / {
        proxy_pass              http://api.foursquare.com/;
        proxy_redirect          off;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

foursquare.yourdomainname.com根据情况自行设定
此配置仅用作手机端自定义API

PHP获取给定IP网段信息

已更新,请移步至《ipInfo.class.php更新》

刚写了段代码, 主要功能是通过给定的网段信息(如10.0.0.1/22), 获取该网段的起始IP地址, 掩码IP地址和广播地址等的信息. 写这段代码的目的是做IP地址查询, 主要用于查询大型集团式企业日志服务器中成百上千个设备中的某一个是归属于哪一个分支企业的(分支企业按一定规则划分了不同的网段). 代码如下:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php 
/* 
* Leon 
* http://nerrsoft.com * leon@nerrsoft.com 
* 2010-08-11 
*/ 
class ipInfo{ //根据给定的IP字串获取IP信息 
    public function getIpInfo($ipStr){ 
        if(!$this->valid($ipStr)){ 
            return false; 
        } 
        $ipArr = explode('/', $ipStr); 
        //information 
        $info['ipStr'] = $ipStr; 
        $info['bin']['mask'] = $this->getSubnetMask($ipArr[1]); 
        $info['ip']['mask'] = $this->bin2ip($info['bin']['mask']); 
        $info['long']['mask'] = ip2long($info['ip']['mask']); 
        $info['bin']['net'] = $this->ip2bin($ipArr[0]) & $info['bin']['mask']; 
        $info['ip']['net'] = $this->bin2ip($info['bin']['net']); 
        $info['long']['net'] = ip2long($info['ip']['net']); 
        $info['ip']['begin'] = long2ip($info['long']['net']+1); 
        $info['bin']['begin'] = $this->ip2bin($ipArr[0]); 
        $info['long']['begin'] = ip2long($info['ip']['begin']); 
        $info['ip']['end'] = long2ip(abs($info['long']['mask'])+$info['long']['begin']-3); 
        $info['bin']['end'] = $this->ip2bin($info['ip']['end']); 
        $info['long']['end'] = ip2long($info['ip']['end']); 
        $info['ip']['broacast'] = long2ip($info['long']['end']+1); 
        $info['bin']['broacast'] = $this->ip2bin($info['ip']['broacast']); 
        $info['long']['broacast'] = ip2long($info['ip']['broacast']); 
        return $info; 
    } 
    //验证IP字串格式有效性 10.0.0.1/24 
    private function valid($ipStr){ 
        if(preg_match("/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\/\d{1,2}$/", $ipStr)){ 
            return true; 
        }else{ 
            return false; 
        } 
    } 
    //获取二进制 
    private function get_bin($number){ 
        return str_pad(decbin($number),8,'0',STR_PAD_LEFT); 
        return decbin($number); 
    } 
    //IP地址转二进制 
    private function ip2bin($ip){ 
        $ip_octets = split("\.", $ip);
        unset($bin_sn); 
        foreach($ip_octets as $val){ 
            $bin_sn[] = $this->get_bin($val);
        } 
        return join(".", $bin_sn); 
    } 
    //二进制转IP地址 
    private function bin2ip($ip){ 
        $ip_octets = split("\.", $ip); 
        unset($bin_sn); 
        foreach($ip_octets as $val){ 
            $bin_sn[] = bindec($val); 
        } 
        return join(".", $bin_sn); 
    } 
    //获取子网掩码 
    private function getSubnetMask($mask){ 
        for($i=1; $i<=32; $i++){ 
            if($i<=$mask){ 
                $maskStr .= '1'; 
            }else{ 
                $maskStr .= '0'; 
            } 
            if($i%8==0){ 
                $maskStr .= '.'; 
            } 
        } 
        $maskStr = substr($maskStr, 0, -1); 
        return $maskStr; 
    } 
} 
//example 
$ip = new ipInfo(); 
$result = $ip->getIpInfo('10.229.8.10/22'); 
print_r($result); 
?>