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

php开发安全程序

2009年03月29号
查看评论 发表评论 855次浏览

用户输入验证
一个非常重要的技术是如何从用户的输入中有效验证数据,从而保护我们的网站程序与数据。我们需要检查用户输入的全部内容:不管这个数据是从Cookies,GET方法或POST方法来的数据,我们都要检验。
除了在php.ini文件中把register_globals设置为Off外,还要把错误级别修改为E_ALL | E_STRICT,这样就可以阻止从外部请求的数据中生成全局变量,后面的设置是把错误级别设置为打开初始化变量的警告错误。对于不同类型数据的表单提交,我们可以使用不同的方法来处理。如果我们需要一个参数通过URL的GET方法提交一个整型值,那么在程序中就要把这个参数强制转换为一个整型值,如以下代码所示。

<?php

if (!isset($_GET['prod_id'])) {

echo “错误, product ID不能为空”;

}else{

/* 强制转换为数值型变量 */

$product_id = (int)$_GET['prod_id'];

}

?>

高级数据验证:使用ctype
除了上面的方法,PHP 5还提供了一组叫做ctype的外部扩展库,它提供一个非常迅速的机制,专门针对字符串内容的校验功能。

我们看一个应用ctype扩展的例子:

<?php

if (!ctype_alnum($_GET['login'])) {

echo “输入项必须为英文字符或数字0-9。”;

}

if (!ctype_alpha($_GET['captcha'])) {

echo “输入项必须为英文字符(大小写均可)”;

}

if (!ctype_xdigit($_GET['color'])) {

echo “输入项必须填写一个16进制数字。”;

}

?>

数据过滤 - PECL filter扩展
PHP 5的PECL扩展库提供了一个新功能-filter扩展。使用filter可以极大的简化表单验证的编码量,尤其是对PHP新手而言,对提高程序的安全性很有帮助,从而远离SQL注入和不充分的字符过滤处理。

页面: 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. 目前没有通告