您当前位置: 首页 »

PHP

标签归档: PHP

在 Windows 下为 PHP 5.4 安装 PEAR、PHPUnit 及 phpDoc2

1.安装PEAR

官方网站: http://pear.php.net/

PHP 5.4 的 Windows 包中没有自带 PEAR,下载 http://pear.php.net/go-pear.phar 到PHP目录,并运行:

php go-pear.phar

安装完成后PHP目录下会产生一个名为 pear.bat 的批处理文件,并且会在系统中添加以下环境变量(用户变量,假定PHP安装在C:\php):

PHP_PEAR_BIN_DIR=C:\php
PHP_PEAR_DATA_DIR=C:\php\data
PHP_PEAR_DOC_DIR=C:\php\docs
PHP_PEAR_INSTALL_DIR=C:\php\pear
PHP_PEAR_PHP_BIN=C:\php\php.exe
PHP_PEAR_SYSCONF_DIR=C:\php
PHP_PEAR_TEST_DIR=C:\php\tests

在写使用了 PHP 的批处理脚本时可以好好利用下这些环境变量。

在命令行输入:

pear version

如果有显示类似如下信息,说明 PEAR 已经成功安装:

PEAR Version: 1.9.4
PHP Version: 5.4.4
Zend Engine Version: 2.4.0
Running on: Windows NT ACER-TM4750G 5.1 build 2600 (Windows XP Professional Service Pack 3) i586

2.安装 PHPUnit

官方网站: http://www.phpunit.de/
PHPUnit 3.6 需要 PHP 5.2.7 (或更改版本) 但强烈推荐使用 PHP 5.3.9 (或更高版本)。

PHPUnit 使用PEAR进行安装:

pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit

安装完成后PHP目录下会产生一个名为 phpunit.bat 的批处理文件。

在命令行输入:

phpunit --version

如果有显示类似如下信息,说明 PHPUnit 已经成功安装:

PHPUnit 3.6.11 by Sebastian Bergmann.

注意,在 PHP 官方 PEAR 频道上也有两个 PHPUnit 的包,名称分别为 PHPUnit 和 PHPUnit2 ,这两个包都是旧版本,分别是 1.x 和 2.x 的版本,如果你使用下面的方式直接安装:

pear install phpunit

那你安装的就是为 PHP 4.x 提供的旧版本 PHPUnit 1.x 了,要使用最新的 PHPUnit 3.x 版本,你需要使用 pear.phpunit.de 这个频道。

使用PHPUnit:

%PHP_PEAR_BIN_DIR%\phpunit test.php

3.安装 phpDocumentor 2

官方网站: http://www.phpdoc.org/
phpDocumentor 2 是为PHP 5.3 或更高版本的所有特性生成 API 文档而创建。

phpDocumentor 2 同样使用PEAR进行安装:

pear channel-discover pear.phpdoc.org
pear install phpdoc/phpDocumentor

安装完成后PHP目录下会产生一个名为 phpdoc.bat 的批处理文件。

在命令行输入:

phpdoc --version

如果有显示类似如下信息,说明 phpDocumenter 2 已经成功安装:

phpDocumentor version 2.3.1

使用方法,在要生成文档的项目所在目录运行:

%PHP_PEAR_BIN_DIR%\phpdoc -d . -t docs

PHP 5.4 中经 htmlspecialchars 转义后的中文字符串为空的问题

PHP 5.4.3 环境中测试了一个在 PHP 5.2 环境下运行正常的程序,却发现本应正常提交一个中文字符串到数据库的代码却提交了一个空字符串,经过排查,该字符串在经 htmlspecialchars 函数转义之前正常,而在转义之后却变成了空字符串。调用例子如下:

$str = '中文字符串';
$str_converted = htmlspecialchars($str);
echo $str_converted;

遂查看PHP手册,获知 htmlspecialchars 函数原型如下:

string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' [, bool $double_encode = true ]]] )

更新日志里面又有提到:

5.4.0 The default value for the encoding parameter was changed to UTF-8.  
5.4.0 The constants ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_XML1, ENT_XHTML and ENT_HTML5 were added. 

PHP 从 5.4.0 版本开始第三个参数字符串编码的默认值改成了 UTF-8,而我这段代码中的中文编码正好是 GB2312 编码的,跟现在的默认参数不一致,于是更改调用参数如下:

$str = '中文字符串';
# 为了与旧环境兼容,这里第二个参数没有组合使用 PHP 5.4 新加入的 ENT_HTML401 常量
$str_converted = htmlspecialchars($str, ENT_COMPAT ,'GB2312');
echo $str_converted;

这样,“中文字符串”就可以正常显示了。为了使 PHP 5.4 之前环境中编写的代码能够向前兼容,建议调用 htmlspecialchars 函数的的时候都提供字符串编码参数。

参考: http://php.net/htmlspecialchars

CentOS 5.6 中为 PHP 安装 Suhosin (扩展形式)

0.安装php-devel包

yum install php-devel

1.下载 suhosin 0.9.29 版本(经测试,0.9.30 以上版本与CentOS中的PHP 5.1.6不兼容)

wget http://download.suhosin.org/suhosin-0.9.29.tgz

如果你使用的是 PHP 5.3 版本,可以到这里找到最新的suhosin源码下载地址
Suhosin Downloads: http://www.hardened-php.net/suhosin/download.html

2.解压、编译并安装

tar xvfz suhosin-0.9.29.tgz
cd suhosin-0.9.29
phpize
./configure
make
make install

3.把suhosin扩展加入到PHP配置文件

echo 'extension=suhosin.so' > /etc/php.d/suhosin.ini

4.重启 Apache 以加载 suhosin 扩展

service httpd restart

x.确认 Suhosin 已安装

php -v

查看是否有以下信息:
with Suhosin v0.9.29, Copyright (c) 2007, by SektionEins GmbH

或者在phpinfo()信息中查找 suhosin 扩展信息(与编译安装suhosin时不一样,这里不是和Zend信息显示在一起的)

参见:

  1. http://www.cyberciti.biz/faq/rhel-linux-install-suhosin-php-protection/
  2. http://www.howtoforge.com/how-to-harden-php5-with-suhosin-on-centos-5.4

nusoap 客户端获取的中文数据变成问号的问题

问题描述:
使用 nosoap_client 类创建了一个客户端 SOAP 对象,然后从 $client->call 方法传回UTF-8编码的中文数据时,所有的中文数据均显示为问号(nusoap 版本为 0.9.5)

问题原因:
nusoap_client 的 decode_utf8 属性默认设置为 true,导致默认对UTF-8数据进行转码,在 nusoap 中的具体调用过程如下:

class.soapclient.php 中的 nusoap_client 的 parseResponse 在解析返回的数据时使用了 nusoap_parser 类

		$parser = new nusoap_parser($data, $this->xml_encoding, $this->operation, $this->decode_utf8);

class.soap_parser.php 中 soap_parser 类的 character_data 方法中又调用了utf8_decode进行了转码

		if ($this->xml_encoding=='UTF-8'){
			// TODO: add an option to disable this for folks who want
			// raw UTF-8 that, e.g., might not map to iso-8859-1
			// TODO: this can also be handled with xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
			if($this->decode_utf8){
				$data = utf8_decode($data);
			}
		}

解决方法:
在使用 $client = new nusoap_client() 创建了对象之后马上设置 decode_utf8 为 false;

设置方法如下:

$client->decode_utf8 = false;

$client->decodeUTF8(false);

补充:
又遇到一则因服务端输出信息编码不匹配,导致 NuSOAP XML Parser 以’ISO-8859-1’编码解析而出错的问题,NuSOAP 库相关部分代码在 class.nusoap_base.php 中,具体位置如下:

    /**
	* charset encoding for outgoing messages
	*
	* @var      string
	* @access   public
	*/
    var $soap_defencoding = 'ISO-8859-1';
	//var $soap_defencoding = 'UTF-8';

解决方法:

$client->soap_defencoding = 'utf-8';	# 仅比上述问题增加该项设置
$client->xml_encoding = 'utf-8';	# 默认值
$client->decode_utf8 = false;

参考链接:
http://blog.csdn.net/mynamesucks/archive/2006/05/26/756480.aspx
http://lhx1026.javaeye.com/blog/506092
http://plog.longwin.com.tw/programming/2010/03/16/php-soap-nusoap-client-2010

Apache崩溃提示php5ts.dll出错的问题

现象:

Apache在Windows启动后自动启动服务时出错,跳出错误报告,提示php5ts.dll导致Apache崩溃,同时,所有基于MySQL的PHP程序无法运行,例如phpMyAdmin一登录就出错

原因:

PATH环境变量设置中将MySQL的安装路径也加入在里面,而且MySQL的路径在PHP的路径前面,导致PHP的MySQL模块使用了MySQL安装目录下的libMySQL.dll而不是PHP安装目录下的libmysql.dll,而根据PHP安装目录中install.txt在第966行左右的警告:

Warning

Don’t mix up your installation with DLL files from different PHP
versions. You have the only choice to use the DLL’s and extensions that
ship with your downloaded PHP version.

这个警告说只能使用你下载PHP时一起下载过来的版本的DLL,而上面的设置恰恰混合使用了MySQL里面的DLL和PHP自己的DLL,因而导致了这个问题的产生。

解决方法:

将 Path 环境变量中的 PHP 路径放到 MySQL 的前面,或者如果你不需要直接在命令行中使用 mysql 的话(仍然可以切换到MySQL目录下再访问mysql命令行程序,只是不那么方便)可以去掉 MySQL 在 Path 环境变量的路径