好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

谈CI框架的安全性

用过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) ">" &#39; "</span>, <span style=" color:rgb(255,0,0) ">" &quot; "</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>;  


查看更多关于谈CI框架的安全性的详细内容...

  阅读:74次

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://www.haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网
[ SiteMap ]