我的URL路径:如何避免由“+”符号引起的错误GET请求?
https://mypage/main.php?dir=farm/1234_animal_fred+sam_cats
我请求用GET方法的数据:
$dir = $_GET['dir'];
echo $dir;
但我的结果是
farm/1234_animal_fred sam_cats
我的URL路径:如何避免由“+”符号引起的错误GET请求?
https://mypage/main.php?dir=farm/1234_animal_fred+sam_cats
我请求用GET方法的数据:
$dir = $_GET['dir'];
echo $dir;
但我的结果是
farm/1234_animal_fred sam_cats
即使Unex公司的答案是正确的,这种特殊情况下,我不确定他是否会丢失其他字符可能在您的数据库中可能会导致某些内容失败,或者更糟糕的是,可能会导致您遇到XSS漏洞。因此,而不是使它在这一个单一的情况下,通过做工作:
$url = str_replace('+', '%2B', $url);
你想,而不是使之成为所有箱子工作。所以,假设你已经从你的数据库中的以下内容:
$file_path = 'farm/1234_animal_fred+sam_cats';
要正确地做这项工作,你需要做的urlencode()
URL的一部分,但是从this answer你会发现,这是不够的,保护你免受XSS攻击。因此,你还需要在字符串中使用htmlspecialchars()
:
$url = 'https://mypage/main.php?dir=';
$file_path = 'farm/1234_animal_fred+sam_cats';
$url .= htmlspecialchars(urlencode($file_path));
echo $url; // https://mypage/main.php?dir=farm%2F1234_animal_fred%2Bsam_cats
和去这个网址时,你可以看到,PHP得到正确的值:
print_r($_GET) output:
Array
(
[dir] => farm/1234_animal_fred+sam_cats
)
非常感谢!这对我很有帮助! – Jarla
你应该总是使用原料进行urlencode( $ url)来编码你的网址。
http://php.net/manual/en/function.rawurlencode.php
现金去提意见,对不起你们我是为了迅速作出反应。
请勿在您的网址中使用+
,而应使用%2B
。
这是结果:
https://mypage/main.php?dir=farm/1234_animal_fred%2Bsam_cats
希望这有助于,
这是我的意见 – Unex
不要在你的URL中使用'+'但是'%2B'代替; https://mypage/main.php?dir = farm/1234_animal_fred%2Bsam_cats – Unex
好的,非常感谢! – Jarla
“1234_animal_fred + sam_cats”零件是如何生产的? – Mike