Home > Backend Development > PHP Tutorial > php代码优化

php代码优化

WBOY
Release: 2016-06-23 14:30:39
Original
974 people have browsed it

 下面这一小段“劣质”的PHP代码是一道简化了的测试题。这种问题就像在问:你该怎样优化这段代码?


echo(”

Search results for query: ” .
    $_GET['query'] . “.

”);
?>

  这段代码的主要问题在于它把用户提交的数据直接显示到了网页上,从而产生XSS漏洞。其实有很多方法可以填补这个漏洞。那么,什么代码是我们想要的呢?


echo(”

Search results for query: ” .
    htmlspecialchars($_GET['query']) . “.

”);
?>

  这是最低要求。XSS漏洞用htmlspecialchars函数填补了,从而屏蔽了非法字符。

if (isset($_GET['query']))   
{   
  echo ‘

Search results for query: ‘,   
      htmlspecialchars($_GET['query'], ENT_QUOTES), ‘.

’;   
}   
?> 

  能写出这样代码的人应该是我想要录用的人了。

  可惜的是,能给出这样让人满意答复的程序员少之又少
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template