如何检查由URI动态生成的变量名的有效性


How to check validaty of variables names dynamically generated by URI

我正在为我的自定义MVC项目构建路由器。

在我的路由器中,对于漂亮的URL名称,我遇到了问题。处理通过URI动态生成的变量名的最佳实践是什么?

的例子:http://knjiskicrv.comoj.com/book/id/2/

将生成:

$page = 'book';
$id = '2';

现在,当有人故意开始弄乱URI时,问题就出现了。如:

http://knjiskicrv.comoj.com/book/id + 1/2/

我会得到:

$page = 'book';
$id one = '2';

希望有人能给我一些建议如何预防和解决这个问题?谢谢。

我想你是在问如何减轻"跨站脚本"(XSS)漏洞。

这是个大话题。记住:对于一个(潜在恶意的)用户来说,有很多方法可以"故意开始捣乱……

建议:开始阅读:)

以下是一些链接:

http://seancoates.com/blogs/xss-woes

http://www.cgisecurity.com/xss-faq.html

http://www.uri.edu/webservices/phpGuideline.html

首先,对url进行输入过滤。不要从可欺骗的输入源创建动态变量。你必须知道,在给定的页面上可以看到什么。这些变量包含哪些变量和变量类型。

如果你要显示一组类别而其中一个类别的名称是'id'怎么办

/products/monkeys/white/id/- you are proper .d

选择一个不同的约定来处理您的URI。

就像把URI分成区域、部分和页面元素。

http://www.oink.com/products/pigs/spottyones/angry/the_big_spotty_pig.html

area = 'products'
section = array('spottyones','angry')
Page = the_big_spotty_pig(唯一标识文章、产品等)

当我必须使用变量时,这些主要是关于排序,页面编号等。因此,这些可以作为查询字符串参数附加。

更新卫生处理:

你必须为自己设定规则。假设URI只能包含某些字符。

//Sanitization 
$uri = $_SERVER['REQUEST_URI']; // /products/monkey/angry/page.html
//allow only characters, numbers, underline and dash 
if (!preg_match('~^[a-z0-9-_]$~isD',$uri)) 
$uri = '/'; //URI has been tampered with
$uriparts = explode('/',$uri); 
/* array('products','monkey','angry','page.html') */
//Do whatever you want with the uri parts ...

您可以将这些变量存储在数组中,因此您得到

$var['id one'] = '2';

这是我的建议