首页 > PHP > php开发安全程序

php开发安全程序

2009年03月29号
查看评论 发表评论 1,612次浏览

第3个例子比较实用,这个脚本代码可验证IP是否正确,并确认是否是内部网IP,代码如下所示。

<?php

// 验证IP v4地址

$ip = “192.168.0.23″;

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === False){

echo “$ip 是非法IP地址”;

}else {

echo “$ip 是正确的IP地址”;

//验证IP是公网IP还是私有IP地址

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) === FALSE)

{

echo “$ip 为内部网私有IP地址”;

}else{

echo “$ip 为公网IP地址”;

}

}

?>

因为PHP 5.2之后已经捆绑了filter扩展,但这一功能一直在升级,你可以查看最新的input_filter动态以及源代码,包括PHP创始人lerdof的个人网站:http://lerdorf.com/php/input_filter.txt

input_get()函数
input_get()函数用来获取外部变量,如POST,GET全局数组,格式如下:

mixed input_get ( int type,string variable_name [,int filter [,mixed flags [,string charset]]] )

其中的几个参数含义如下所述。

type:该参数值可以是以下几个值中的一个,它们是INPUT_GET,INPUT_POST,INPUT_COOKIE,INPUT_SERVER,INPUT_ENV,INPUT_SESSION,还有一个99,目前是用来对$_REQUEST进行过滤。

variable_name:变量名。

filter:同filter_data,默认值为FILTER_DEFAULT。

flags:过滤标志。

charset:使用的字符集。

下面是一个使用input_get()函数的例子,代码如下:

<?php

$search_html = input_get(INPUT_GET, ‘search’, FILTER_SANITIZE_SPECIAL_CHARS);

$search_url = input_get(INPUT_GET, ‘search’, FILTER_SANITIZE_ENCODED);

echo “您已经搜索到 $search_html.\n”;

echo “<a href=’?search=$search_url’>重新查询.</a>”;

?>

该程序输出为:

您已经搜索到e &#38; son.<br />

<a href=’?search=Me%20%26%20son’>重新查询.</a>

在上面的代码中,$_GET['search']分别被两种不同过滤器过滤,产生的值也不相同,第一行的filter执行了类htmlspecialchars的操作,第二行则进行了urlencode操作。

说明:在PHP 5.1.4版本之前,不能用filter扩展库。

路径检测
PHP应用程序应该安全地进行文件处理,在做操作时一定要进行验证,避免存取文件时访问一些系统或安全级别较高的文件,比如,用户使用如下的URL访问:

http://www.ourwebsite.com/script.php?path=../../etc/passwd

而我们的程序是这样写的:

<?php

$fp = fopen(“/home/dir/{$_GET['path']}”, “r”);

?>

这样的代码将允许打开服务器系统任意目录下的文件,这是异常危险的。

PHP中有一个basename()的函数,可以用它来引导一个路径并移动每个文件,请见如下代码:

<?php

$_GET["path"] = basename($_GET['path']);

//只有文件存在的情况才能打开文件

if (file_exists(“/home/dir/{$_GET['path']}”)) {

$fp = fopen(“/home/dir/{$_GET['path']}”, “r”);

}

?>

最好的解决方法是建立这个用户可以使用的文件白名单,我们在一个模板上建立允许打开的文件,如果有,则允许用户打开该文件。

页面: 1 2 3 4

类别PHP 标签
  1. mulberry sale
    发表于 2010年06月14号 14时12分56秒 | 1楼

    i hope so

  2. cheap timberland boots
    发表于 2010年06月16号 03时55分44秒 | 2楼

    what a nice post.what a great website :o ) thanks

  3. purple ghd iv styler
    发表于 2010年06月16号 07时43分07秒 | 3楼

    This is a fine example.

  4. Kooba Handbags
    发表于 2010年06月21号 13时15分03秒 | 4楼

    I guess I had to think more about what you wrote before i can comment on it..

  5. mulberry bag
    发表于 2010年06月22号 17时17分05秒 | 5楼

    Have a great day.
    LOADS OF SUNSHINE TO BE ON YOU TODAY.

  6. bikini swimwear
    发表于 2010年07月10号 16时17分04秒 | 6楼

    Whoop whoop. I have a feeling this is becoming the best.Keep it up!:)

  7. skechers kids
    发表于 2010年07月19号 17时50分59秒 | 7楼

    Even though it will not be here tomorrow, the effect of it will.

  8. lebron basketball shoes
    发表于 2010年07月19号 20时36分28秒 | 8楼

    Hallelujah!

  9. timberland 6 inch boots
    发表于 2010年07月24号 21时55分08秒 | 9楼

    These words mean a great deal to me today. Truly, it is necessary to let go – I see that.

  10. Louboutin sale
    发表于 2010年07月25号 12时11分16秒 | 10楼

    Well that’s a honest and true quotation

  11. discount puma shoes
    发表于 2010年08月02号 14时20分37秒 | 11楼

    It’s heroic.It is as the candle burns itself to give light to others.

  1. 目前没有通告
你必需 登陆 才能发表评论.