2012-03-20 40 views
1

我目前正在为PHP中的留言簿写一个PHP脚本,用户可以在表单中放置他们的姓名,网站和消息,但是我想阻止某人将javascript: //在德网址框,以减少XSS的风险,我试着来解决这个问题:如何阻止javascript://作为url参数

<?php filter_var($_POST['website'], FILTER_VALIDATE_URL) ?> 

但我仍然能够把JavaScript代码://在德URL框中我怎么能避免这种情况?

+1

因为这是一个有效的网址。你必须解析url并检查使用的协议。 – 2012-03-20 18:37:41

回答

-1

用的preg_replace

$website = preg_replace("/(javascript:\/\/)/i", "http://", mysql_real_escape_string($_POST['website'])); 

或str_ireplace

$website = str_ireplace("javascript:", "http:", mysql_real_escape_string($_POST['website'])); 
+1

如果将$ Website放入,那么如果您使用html对javascript中的任何字母进行编码,则会绕过您的替换。 java s cript:alert(1) – Erlend 2012-03-21 06:24:01

2
$chk = parse_url($url); 

switch ($chk['scheme']) { 
case 'http': 
case 'https': 
    break; 
default: 
    // throw error here 
    break; 
} 
3

甲清洁溶液将使用PHP的parse_url功能并检查所使用的协议是HTTP或在允许的一个列表协议,如果您允许http://和skype://例如...

$url = 'http://username:[email protected]/path?arg=value#anchor'; 
$tmp = parse_url($url); 

if ($tmp['scheme'] === 'http') { 
    echo 'is http://'; 
} 

if (in_array($tmp['scheme'], array('http', 'skype', 'call')) { 
    echo 'is allowed protocol'; 
} 
+0

感谢你们,我没有使用parse_url() – 2012-03-20 18:56:02