再次加载页面,便会弹出一个警告框

在FireFox中进行同样的测试,成功执行。

现在将X-XSS-Protection头的值修改为1,再次尝试。
header("X-XSS-Protection: 1");
你能够轻松体会到已经成功开启了X-XSS-Protection。
HTTP/1.1 200 OK
Date: Sun, 12 Apr 2015 14:54:42 GMT
Server: Apache/2.2.29 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.8 PHP/5.6.2 mod_ssl/2.2.29 OpenSSL/0.9.8y DAV/2 mod_perl/2.0.8 Perl/v5.20.0
X-Powered-By: PHP/5.6.2
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-cookie: PHPSESSID=8dfb86b13ec9750d1f1afdfc004f5042; path=/
X-XSS-Protection: 1
Content-Length: 820
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
再次在漏洞页面执行Javascript,脚本不会执行。进入Chrome的控制台,看看发生了什么事

从上面的控制台信息中,我们可以得知脚本没有得到执行。
header("X-XSS-Protection: 1");
上面这个头没有添加其他的参数,仅仅只是阻止脚本的执行。
我们可以添加一些其他参数,比如:
header("X-XSS-Protection: 1; mode=block");
这时再次测试,浏览器会阻止脚本执行,并且返回一个空白页。

下面为HTTP头信息
HTTP/1.1 200 OK
Date: Mon, 13 Apr 2015 09:59:22 GMT
Server: Apache/2.2.29 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.8 PHP/5.6.2 mod_ssl/2.2.29 OpenSSL/0.9.8y DAV/2 mod_perl/2.0.8 Perl/v5.20.0
X-Powered-By: PHP/5.6.2
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-cookie: PHPSESSID=729f2f716310ccfe353c81ced1602cf0; path=/
X-XSS-Protection: 1; mode=block
Content-Length: 846
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
尽管在一些主流浏览器(IE,Chrome,Safari)中可以完美实现。但在FireFox中并不支持这个头,所以我们仍然可以看到弹出警告框

总结
所以,X-XSS-Protection头应该用于深度防御。由于它无法完全保护网站,因此开发者必须确保他们有其他一些手段来进行防护。