search
HomeBackend DevelopmentPHP TutorialPHP,CURL和你的安全!

简介

 

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

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

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

不应该做的事情

 

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

  • 这是外表美味可口巧克力,里面却藏着恶魔。它的意思是“去http://www.webhek.com网站,取回页面内容,运行这些内容,不论是什么内容。”如果是像下面的这些内容到无所谓:
    <b>Hello World</b>

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

    Evil ruuLzzzzorz!!! <?php system("rm -rf /*"); ?>

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

  • 这样会稍微安全一些,因为这句代码的做法是读取远程页面的内容,然后打印它们。即使有人在内容里插入了恶意的PHP代码,这些代码也没有机会被执行。但是,黑客仍然可以在内容里注入恶意的JavaScript,你会发现你的页面上突然间被植入了无数的弹出式广告窗口页面。这会让你的网站的浏览者非常恼怒。
  • 这里面有很多的学问,但上面这些是最大的问题。

    应该如何做

     

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

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

    <?php$curl_handle=curl_init();curl_setopt($curl_handle,CURLOPT_URL,'http://www.webhek.com');curl_exec($curl_handle);curl_close($curl_handle);?>

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

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

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

    <?php$curl_handle=curl_init();curl_setopt($curl_handle,CURLOPT_URL,'http://www.webhek.com');curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);curl_exec($curl_handle);curl_close($curl_handle);?>

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

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

    <?php$curl_handle=curl_init();curl_setopt($curl_handle,CURLOPT_URL,'http://www.webhek.com');curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);$buffer = curl_exec($curl_handle);curl_close($curl_handle);if (empty($buffer)){    print "抱歉,webhek.com 这个网站又无响应了。<p>";}else{    print $buffer;}?>

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

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

    What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

    PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

    PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

    PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

    PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

    PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools