我有输入一个完整的文章中Laravel过滤,你可能会发现它有用http://usman.it/xss-filter-laravel/,这里是本文摘自:
如果您没有库来编写可能需要的常用方法,那么您可以自己进行全局XSS清理,然后请您在应用程序/库中创建一个新库。将这个两个方法在公共库:
/*
* Method to strip tags globally.
*/
public static function global_xss_clean()
{
// Recursive cleaning for array [] inputs, not just strings.
$sanitized = static::array_strip_tags(Input::get());
Input::merge($sanitized);
}
public static function array_strip_tags($array)
{
$result = array();
foreach ($array as $key => $value) {
// Don't allow tags on key either, maybe useful for dynamic forms.
$key = strip_tags($key);
// If the value is an array, we will just recurse back into the
// function to keep stripping the tags out of the array,
// otherwise we will set the stripped value.
if (is_array($value)) {
$result[$key] = static::array_strip_tags($value);
} else {
// I am using strip_tags(), you may use htmlentities(),
// also I am doing trim() here, you may remove it, if you wish.
$result[$key] = trim(strip_tags($value));
}
}
return $result;
}
然后把这个代码在你之前,过滤器的开始(在应用程序/ routes.php文件):
//Our own method to defend XSS attacks globally.
Common::global_xss_clean();
删除了答案,因为它没有应用到XSS。我认为没有硬性或快速的方法...过滤内容,将Cookie与IP地址绑定。Wiki条目具有很好的信息负载http: //en.wikipedia.org/wiki/Cross-site_scripting –
@DavidBarker,你让我错了。我需要的只是在显示用户输入时防止执行标签和JavaScript。 – warmspringwinds