【问题标题】:PHP - I get "Bad Request" Error when using $_GET with stringsPHP - 将 $_GET 与字符串一起使用时出现“错误请求”错误
【发布时间】:2014-10-24 06:03:54
【问题描述】:

我正在尝试使用 $_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

如何解决?因为这是我实现它的必要条件!

【问题讨论】:

  • 你能告诉我你的网站是做什么的吗?
  • urlencode()你的参数
  • 3行代码被学分包围,笑死我了;p
  • @LozCheroneツ 你什么意思?
  • @LozCheroneツ 好的,我明白了。我也是。也许这是他们的第一个节目

标签: php string get url-parameters


【解决方案1】:

“错误请求”(通常是 400 状态代码)告诉您,您(客户端)向服务器发送了无法理解的输入。因此,您的脚本没有问题,但您传递给它的内容没有问题。

正如您问题下方的 cmets 所指出的,这可能是因为您向服务器发送了格式错误的请求字符串。查询参数(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

有关在线 urlencoder/解码器,请参阅 this tool。 PHP 有一个内置函数:urlencode 将空格编码为 '+' 或 rawurlencode 将空格编码为 '%20'(服从 RFC3986)

【讨论】:

  • 空格也可以编码为+。然后变成...index.php?data=This+is+a+set+of+data...
  • 当我使用url-encoded 参数执行此操作时,它将在浏览器中转换为普通参数(已解码),再次出现404错误!
  • 我真的不知道你在说什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 2023-03-07
  • 2020-08-18
  • 2012-07-16
  • 2014-05-03
  • 2021-06-25
相关资源
最近更新 更多