PHP-我得到“;坏请求”;将$_GET与字符串一起使用时出错


PHP - I get "Bad Request" Error when using $_GET with strings

我正试图使用$_GET获取两个参数,如下所示:

index.php:

<?php
include("../includes/simple_html_dom.php");
echo ("<!-- Written By: Alaa Elrifaie -->'n");
// Fetch user input
$data = htmlspecialchars($_GET["data"]);
$key = htmlspecialchars($_GET["key"]);
// Test if values have been fetched
echo($data . '<br>' . $key);
?>

当传递这样一个单词时,它工作得很好:

http://www.mywebsite.com/index.php?data=OneWord&key=AlsoOneWord

但当试图传递完整字符串时,它会给出"404错误":

http://www.mywebsite.com/index.php?data=This is a set of data&key=This is the key!
// This doesn't work

如何解决?因为实现它对我来说是必要的!

"错误请求"(通常是400状态代码)告诉您(客户端)向服务器发送了无法理解的输入。所以你的脚本没有错,只是你传递给它的东西有错。

正如你的问题下面的评论所指出的,这种情况可能是因为你向服务器发送了一个格式错误的请求字符串。查询参数(URL中"?"之后的所有内容)必须进行编码,因此不需要

http://www.mywebsite.com/index.php?data=This is a set of data&key=This is the key!
// This doesn't work

你应该试试

http://www.mywebsite.com/index.php?data=This%20is%20a%20set%20of%20data&key=This%20is%20the%20key!
// This should work

请参阅此工具以获取在线URL编码器/解码器。PHP有一个内置的函数:urlencode将空间编码为"+"或rawurlenode将空间编码成"%20"(遵循RFC3986)