« Posts tagged PHP

在Yii中使用活动目录验证用户身份

借助第三方类库adLDAP,将adLDAP.php复制到项目的vendors目录中。修改UserIdentity.php

PHP
<?php
 
/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	public function authenticate()
	{   
        $ldapConfig = Yii::app()->params['ldap'];
        Yii::import('application.vendors.*');
        require_once('adLDAP.php');
        try
        {
            $adldap = new adLDAP($ldapConfig);
        }
        catch (adLDAPException $e)
        {
            echo $e; exit();   
        }
        if(!$adldap->authenticate($this->username, $this->password))
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $adInfo = $adldap->user_info($this->username);
            $this->setState('ad_info',$adInfo);
            $this->setState('login_time',time());
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
	}
}

TomcatStats_0.1_PHP – Apache/Tomcat Monitor [Cacti Script and Template]

tomcatstats

基于 Timothy Denike  的 TomcatStats v0.1 修改,去掉了perl的采集脚本,改为PHP实现(SimpleXML)。

Step 1.
配置Tomcat Manager

Step 2.
将采集脚本tomcatstats.php得到到 <cacti_root>/scripts/ 目录下。导入模板cacti_host_template_tomcat_server.xml

Step 3.
调试,在命令行下输入,测试是否可以获取数据。

php tomcatstats.php app1:8444 admin passwords

Step 4.
根据实现设定的账号密码设定Cacti Data Input Methods 中Tomcat Status Input String项

Step 5.
创建图像。

详见附件 »Read More

ipInfo.class.php更新

加入了64位与32位环境的判断(ip2long),更新了计算方法。

PHP
<?php
/*
* Leon
* http://nerrsoft.com
* leon@nerrsoft.com
* 2011-04-11
*/
class ipInfo{
 
    var $address;
    var $netbits;
 
    public function getIpInfo($ipStr){
        list($this->address, $this->netbits) = explode('/', $ipStr);
 
        $info['long']['mask'] = $this->netmask();   
        $info['ip']['mask'] = long2ip($info['long']['mask']);
 
        $info['long']['net'] = $this->network(); 
        $info['ip']['net'] = long2ip($info['long']['net']);
 
        $info['long']['begin'] = $info['long']['net'] + 1;
        $info['ip']['begin'] = long2ip($info['long']['begin']);
 
        $info['long']['end'] = abs($info['long']['mask']) + $info['long']['begin'] - 3;
        $info['ip']['end'] = long2ip($info['long']['end']);
 
        $info['long']['broacast'] = $this->broadcast();
        $info['ip']['broacast'] = long2ip($info['long']['broacast']);
 
        $info['count'] = $info['long']['broacast'] - $info['long']['net'] - 1;
 
        return $info;   
    }
 
    // Return the netmask
    function netmask(){
        $mask = $this->check6432('255.255.255.255');
        return $mask << (32 - $this->netbits);
    }
 
    // Return the network that the address sits in
    function network(){
        $ip = $this->check6432($this->address);
        return ($ip & $this->netmask());
    }
 
    // Return the broadcast that the address sits in
    function broadcast(){
        return ($this->network() | (~$this->netmask()));
    }
 
    // Return the inverse mask of the netmask
    function inverse(){
        $mask = $this->check6432('255.255.255.255');
        return (long2ip(~($mask << (32 - $this->netbits))));
    }
 
    //
    function check6432($ip){
        if(PHP_INT_MAX==2147483647){
          $mask = ip2long($ip);   
        }else{
          list(, $mask) = unpack('l', pack('l', ip2long($ip)));
        }
        return $mask;      
    }
}
?>

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()在某些情况下是不适用的。

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'];
?>

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); 
?>