0

我有一个需要清理一下的链接。

http://'.$_SERVER["SERVER_NAME"]."".$_SERVER["REQUEST_URI"].'" />

此链接将生成如下内容:

http://www.site.com/friends.php

friends.php在哪里$_SERVER["REQUEST_URI"]

有时我将 id 传递给链接:

 http://www.site.com/friends.php?id=123456

我想要的是使用 strip_tags 或 urldecode 清理此链接并确保传入的任何内容都是idint 并且不包含字母,但我需要在原始链接上执行此操作:http://'.$_SERVER["SERVER_NAME"]."".$_SERVER["REQUEST_URI"].'" />

编辑:

我希望清除链接,所以我不能这样做:

http://www.site.com/friends.php?id=<script>alert(TK00000006)</script>
4

2 回答 2

1

这假设您的查询字符串中只有 id :

echo 'http://'.$_SERVER['SERVER_NAME']
    . $_SERVER['SCRIPT_NAME']
    .'?id='
    .filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);

结果 id 来自id=<script>alert(TK00000006)</script>is ?id=00000006


替代答案:

echo 'http://'.$_SERVER['SERVER_NAME']
    . $_SERVER['SCRIPT_NAME']
    .'?id='
    .urlencode($_GET['id']);

//or effectively the same thing using only $_SERVER variables,
//but is much more robust as it handles multiple query parameters:
echo 'http://'.$_SERVER['SERVER_NAME']
    . $_SERVER['SCRIPT_NAME']
    .$_SERVER['QUERY_STRING'];

结果 idid=<script>alert(TK00000006)</script>来自?id=%3Cscript%3Ealert%28TK%29%3C%2Fscript%3E

于 2011-10-12T17:26:52.267 回答
0

如果 ID 不能为零,我会这样做:

$id = (int)$_GET['id'];
if ($id)
...      // valid number > 0
else
...      // invalid
于 2011-10-12T16:37:18.210 回答