用过ci框架的人都知道,ci框架能大大缩短你的代码。其实,ci框架更能提高你网站的安全性。
防止对数据库的攻击
数据输入可能引发许多问题。因为 HTML 和数据库的限制,数据中总包含特定的符号—举例来说,省略符号和引号—可能导致你的数据库遭到攻击,最终得到你无法预料的结果。
解决方案是在把这些数据存入数据库前对这些数据进行相关处理。这样做会浪费一些系统时间,增加一些额外编码。
CI 的表单辅助函数会自动地完成这些工作。因此,当你编写一个输入框时:
PHP 代码
[php] view plain copy
<a href= "http://www.php.net/echo" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" > echo </span></a> form_input<span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(255,0,0)" > 'username' </span>, <span style= "color:rgb(255,0,0)" > 'johndoe' </span><span style= "color:rgb(102,204,102)" >)</span>;
CI 也隐式地执行下列校验函数:
PHP 代码
[php] view plain copy
<span style= "color:rgb(0,0,0)" ><strong> function </strong></span> form_prep<span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(0,0,255)" > $str </span> = <span style= "color:rgb(255,0,0)" > '' </span><span style= "color:rgb(102,204,102)" >)</span> <span style= "color:rgb(102,204,102)" >{</span> <span style= "color:rgb(177,177,0)" > if </span> <span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(0,0,255)" > $str </span> === <span style= "color:rgb(255,0,0)" > '' </span><span style= "color:rgb(102,204,102)" >)</span> <span style= "color:rgb(102,204,102)" >{</span> <span style= "color:rgb(177,177,0)" > return </span> <span style= "color:rgb(255,0,0)" > '' </span>; <span style= "color:rgb(102,204,102)" >}</span> <span style= "color:rgb(0,0,255)" > $temp </span> = <span style= "color:rgb(255,0,0)" > '__TEMP_AMPERSANDS__' </span>; <span style= "color:rgb(128,128,128)" ><em> // Replace entities to temporary markers so that</em></span> <span style= "color:rgb(128,128,128)" ><em> // htmlspecialchars won't mess them up</em></span> <span style= "color:rgb(0,0,255)" > $str </span> = <a href= "http://www.php.net/preg_replace" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" >preg_replace</span></a><span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(255,0,0)" > "/&#(<span style=" color:rgb(0,0,153) "><strong>\d</strong></span>+);/" </span>, <span style= "color:rgb(255,0,0)" > "$temp<span style=" color:rgb(0,0,153) "><strong>\\</strong></span>1;" </span>, <span style= "color:rgb(0,0,255)" > $str </span><span style= "color:rgb(102,204,102)" >)</span>; <span style= "color:rgb(0,0,255)" > $str </span> = <a href= "http://www.php.net/preg_replace" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" >preg_replace</span></a><span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(255,0,0)" > "/&(<span style=" color:rgb(0,0,153) "><strong>\w</strong></span>+);/" </span>, <span style= "color:rgb(255,0,0)" > "$temp<span style=" color:rgb(0,0,153) "><strong>\\</strong></span>1;" </span>, <span style= "color:rgb(0,0,255)" > $str </span><span style= "color:rgb(102,204,102)" >)</span>; <span style= "color:rgb(0,0,255)" > $str </span> = <a href= "http://www.php.net/htmlspecialchars" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" >htmlspecialchars</span></a><span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(0,0,255)" > $str </span><span style= "color:rgb(102,204,102)" >)</span>; <span style= "color:rgb(128,128,128)" ><em> // In case htmlspecialchars misses these.</em></span> <span style= "color:rgb(0,0,255)" > $str </span> = <a href= "http://www.php.net/str_replace" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" > str_replace </span></a><span style= "color:rgb(102,204,102)" >(</span><a href= "http://www.php.net/array" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" > array </span></a><span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(255,0,0)" > "'" </span>, <span style= "color:rgb(255,0,0)" >' "'</span><span style=" color:rgb(102,204,102) ">)</span>, <a href=" http://www.php.net/ array " style=" color:rgb(66,120,136); text-decoration:none "><span style=" color:rgb(0,0,102) ">array</span></a><span style=" color:rgb(102,204,102) ">(</span><span style=" color:rgb(255,0,0) ">" ' "</span>, <span style=" color:rgb(255,0,0) ">" " "</span><span style=" color:rgb(102,204,102) ">)</span>, <span style=" color:rgb(0,0,255) ">$str</span><span style=" color:rgb(102,204,102)">)</span>; <span style= "color:rgb(128,128,128)" ><em> // Decode the temp markers back to entities</em></span> <span style= "color:rgb(0,0,255)" > $str </span> = <a href= "http://www.php.net/preg_replace" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" >preg_replace</span></a><span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(255,0,0)" > "/$temp(<span style=" color:rgb(0,0,153) "><strong>\d</strong></span>+);/" </span>,<span style= "color:rgb(255,0,0)" > "&#<span style=" color:rgb(0,0,153) "><strong>\\</strong></span>1;" </span>,<span style= "color:rgb(0,0,255)" > $str </span><span style= "color:rgb(102,204,102)" >)</span>; <span style= "color:rgb(0,0,255)" > $str </span> = <a href= "http://www.php.net/preg_replace" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" >preg_replace</span></a><span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(255,0,0)" > "/$temp(<span style=" color:rgb(0,0,153) "><strong>\w</strong></span>+);/" </span>,<span style= "color:rgb(255,0,0)" > "&<span style=" color:rgb(0,0,153) "><strong>\\</strong></span>1;" </span>,<span style= "color:rgb(0,0,255)" > $str </span><span style= "color:rgb(102,204,102)" >)</span>; <span style= "color:rgb(177,177,0)" > return </span> <span style= "color:rgb(0,0,255)" > $str </span>; <span style= "color:rgb(102,204,102)" >}</span>
上述函数捕获像[&]这样的特殊字符,以便在你的页面提交时不会造成混乱。你应该知道,有些字符会引起问题。
并不是所有的用户都会中规中矩的输入符合要求的信息,你也不可能知道使用浏览器输入信息的是什么人,他们在想什么,做什么。你可以使用 CI 来防止输入不符合要求的信息。当然,你大可不必知道 CI 是如何在幕后为你做到这一切的,你只需要简单地输入如下代码:
[php] view plain copy
<a href= "http://www.php.net/echo" style= "color:rgb(66,120,136); text-decoration:none" ><span style= "color:rgb(0,0,102)" > echo </span></a> form_input<span style= "color:rgb(102,204,102)" >(</span><span style= "color:rgb(255,0,0)" > 'username' </span>, <span style= "color:rgb(255,0,0)" > 'johndoe' </span><span style= "color:rgb(102,204,102)" >)</span>;
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did27073