标签存档: PHP

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 环境变量的路径

PHP 附件形式的下载

当下载需要用户登录验证权限、并且可以自定义下载的文件名等情况下需要以附件形式下载文件,代码如下:
(由于 poor IE 的特殊性,需要为它做特别处理)

header("Content-type: application/octet-stream");
$isIE and $filename = mb_convert_encoding( $filename, 'GBK' , 'UTF-8' ); # IE 不认识UTF-8的中文文件名,所以转换为GBK编码
header('Content-Disposition: attachment; filename="'.$filename.'"'); # 备注: 文件名要做安全过滤,防止跨站攻击
echo file_get_contents($filepath);