php开发安全程序
用户输入验证
一个非常重要的技术是如何从用户的输入中有效验证数据,从而保护我们的网站程序与数据。我们需要检查用户输入的全部内容:不管这个数据是从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注入和不充分的字符过滤处理。
i hope so
what a nice post.what a great website :o) thanks
This is a fine example.
I guess I had to think more about what you wrote before i can comment on it..
Have a great day.
LOADS OF SUNSHINE TO BE ON YOU TODAY.
Whoop whoop. I have a feeling this is becoming the best.Keep it up!:)
Even though it will not be here tomorrow, the effect of it will.
Hallelujah!
These words mean a great deal to me today. Truly, it is necessary to let go – I see that.
Well that’s a honest and true quotation
It’s heroic.It is as the candle burns itself to give light to others.