PHP,CURL和你的保险

WBOY
發布: 2016-06-13 12:11:49
原創
847 人瀏覽過

PHP,CURL和你的安全!

简介

 

如果最近你在美国看电视,你会经常看到一个广告——一个和蔼友善的家伙说“我希望我的电脑被病毒感染”,“我希望所有我家的照片都被人删除,找不回来。”或“我希望我的笔记本运转的声音听起来像打雷。”

当然,没有一个正常人希望遇到这样的痛苦,但如果你不对自己的电脑采取保护措施,结果就是让黑客得逞。你需要理解,这就像在你家里,车或钱袋子,你不能让它们都敞着口放在外面,你不能认为陌生路人都是可信的。大部分的陌生人并不像你想象的那样友好。

如果没有人告诉你应该怎么做,你很容会犯错误。置之不理是愚蠢的,幸好你读了这篇文章。我要首先假设你不是那么愚蠢的人。

不应该做的事情

 

下面是一个列表,解释了什么不该做,以及为什么。

  • <?php include('http://www.webhek.com'); ?>这是外表美味可口巧克力,里面却藏着恶魔。它的意思是“去http://www.webhek.com网站,取回页面内容,运行这些内容,不论是什么内容。”如果是像下面的这些内容到无所谓:
    <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>b</span><span class="token punctuation">></span></span>Hello World<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>b</span><span class="token punctuation">></span></span>
    登入後複製

    但是,如果你不那么幸运,这个网站被人动过手脚,它的内容被替换成:

    Evil ruuLzzzzorz<span class="token operator">!</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token operator"><</span><span class="token operator">?</span>php <span class="token function">system<span class="token punctuation">(</span></span><span class="token string">"rm -rf /*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter">?></span>
    登入後複製

    这句代码会删除你的电脑上的所有东西。

  • <?php print readfile('http://www.webhek.com'); ?>这样会稍微安全一些,因为这句代码的做法是读取远程页面的内容,然后打印它们。即使有人在内容里插入了恶意的PHP代码,这些代码也没有机会被执行。但是,黑客仍然可以在内容里注入恶意的JavaScript,你会发现你的页面上突然间被植入了无数的弹出式广告窗口页面。这会让你的网站的浏览者非常恼怒。

这里面有很多的学问,但上面这些是最大的问题。

应该如何做

PHP里面有一个非常强大的函数库,它们的目的就是让你安全的从远程网站上取回内容。这些函数被称作CURL。现在,你不要被CURL官方页面上大量的东西吓阻,它实际上非常的简单。

下面是一个简单的替换上面read_file()命令的做法:

<span class="token operator"><</span><span class="token operator">?</span>php<span class="token variable">$curl_handle</span><span class="token operator">=</span><span class="token function">curl_init<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_setopt<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">,</span><span class="token constant">CURLOPT_URL</span><span class="token punctuation">,</span><span class="token string">'http://www.webhek.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_exec<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_close<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token delimiter">?></span>
登入後複製

就是这样,这才是你应该做的,最后一句curl_close()不是必要的。

小心,你仍然有被远程网站上的恶意JavaScript和cookie盗取者袭击的风险。防范这些攻击需要牵涉到更多的内容。如果你想做这些,我建议你使用PHP正则表达式函数里的preg_replace()

假设我们确实要用CURL来做一些事情。假设www.webhek.com这个网站不是那么稳定。它有时候会没有响应,一个页面需要30秒才能拉取成功。对于这种情况,我们的办法是:

<span class="token operator"><</span><span class="token operator">?</span>php<span class="token variable">$curl_handle</span><span class="token operator">=</span><span class="token function">curl_init<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_setopt<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">,</span><span class="token constant">CURLOPT_URL</span><span class="token punctuation">,</span><span class="token string">'http://www.webhek.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_setopt<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">,</span><span class="token constant">CURLOPT_CONNECTTIMEOUT</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_exec<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_close<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token delimiter">?></span>
登入後複製

这种写法是说,2秒钟内如果不能抓取完数据就做超时处理。是的,也许你更愿意设定为1秒就算超时,因为它妨碍你的页面的速度。(注意,不要设置为0,这是告诉curl没有超时限制。)

但是,如果是什么东西都没有取回了,你想显示一个提示信息,这该怎么办?哈哈,简单!

<span class="token operator"><</span><span class="token operator">?</span>php<span class="token variable">$curl_handle</span><span class="token operator">=</span><span class="token function">curl_init<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_setopt<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">,</span><span class="token constant">CURLOPT_URL</span><span class="token punctuation">,</span><span class="token string">'http://www.webhek.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_setopt<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">,</span><span class="token constant">CURLOPT_CONNECTTIMEOUT</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_setopt<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">,</span><span class="token constant">CURLOPT_RETURNTRANSFER</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token variable">$buffer</span> <span class="token operator">=</span> <span class="token function">curl_exec<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">curl_close<span class="token punctuation">(</span></span><span class="token variable">$curl_handle</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">empty<span class="token punctuation">(</span></span><span class="token variable">$buffer</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>    <span class="token keyword">print</span> <span class="token string">"抱歉,webhek.com 这个网站又无响应了。<p>"</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>    <span class="token keyword">print</span> <span class="token variable">$buffer</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token delimiter">?></span>
登入後複製

你有没有开始感觉到CURL的强大之处?

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板