在 $_GET 变量中添加空格有什么问题
例如,如果我的网址看起来像index.php?category=IT%20&%20Soft。然后我尝试打印“$_GET[category]”,我只得到“IT”,而不是“IT & Soft”。这里有什么问题吗?这让我很沮丧。
If I for example have my url looking like index.php?category=IT%20&%20Soft. Then i try to print "$_GET[category]" i only get "IT" and not "IT & Soft". What is wrong here? It's frustrating me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题不在于空格,而在于 & 符号。
请改用
%26
,如下所示:index.php?category=IT%20%26%20Soft
与号表示您正在开始另一个字段(例如
category =IT&subcategory=somethingelse
)。您可以在维基百科上查看保留字符列表。The problem is not the spaces, but the ampersand.
Use
%26
instead, like this:index.php?category=IT%20%26%20Soft
The ampersand indicates that you are beginning another field (like
category=IT&subcategory=somethingelse
). You can see a list of reserved characters on Wikipedia.空格和与号 (&) 无效,需要进行编码才能在参数值中使用。通过“urlencode”运行您的参数以创建 url 安全参数值。
http://php.net/manual/en/function.urlencode.php
Spaces and ampersands (&) aren't valid and need to be encoded for use in parameter values. Run your arguments through 'urlencode' to create url safe parameter values.
http://php.net/manual/en/function.urlencode.php
&
是 HTTP URL 和 URI 中的保留字符(通常称为链接或超文本引用)。这个&
正是定义新的$_GET
条目的点。因此它不再是之前的$_GET
条目的一部分。前一个是您想要使用的。所以事实上,类别中的&
是让您头疼的原因:因此请记住:在 URL 中,
&
字符将一个 URL 参数与其他。但这并不意味着您的问题无法解决。您可以通过 URL 参数传递包含
&
字符的值。有一种非常简单的方法可以将&
字符获取到特定的$_GET
值中。为 Internet 往返打包值
但请注意:您现在可能会查看应用程序的输入级别(即数据进入
$_GET
和其他变量的部分)。但要解决这个问题,您需要查看相反的内容 - 输出级别(即发送 HTML 的位置)。为什么?因为输出将成为脚本的输入。因此,如果输出已经错误,则输入永远无法可供使用。
考虑到您的应用程序发送到浏览器的每个 URL(例如,作为 HTML 链接
的一部分)都是一个新命令,其中包含脚本
的特定参数。 $_GET
变量。从技术上讲,这部分称为 HTTP URI/URL 的query
或queryinfo
部分。因此,如果 URL 的构建方式可能会为您的脚本创建错误的
$_GET
参数,那么就已经太晚了。因此,您可以查看输出以修复根源。因此,您需要查看创建 URL 的位置并执行正确的输出,以便稍后获取
$_GET['category']
。在每个链接输出位置,您需要确保每个值都被正确编码。这就是包含问题解决方案的神奇词。这个术语你最好在字典中查找,而不是在技术文档中查找。
$_GET
参数所需值的编码类型就是所谓的URL 编码 - URL 编码 - 有意义,对吧? URL,编码 = URL 编码。对 URL 的值进行编码实际上会确保参数的值在脚本中再次安全可用,而不是将值分开,甚至最终在技术上破坏 URL。
听起来可能有点复杂,但好消息是,您首先只需要知道 PHP 作为一种 Web 脚本语言提供了一个为您执行此编码的函数。它被称为
urlencode()
你可以使用它任何值来创建您的 URL。事实上,这是在 Web 应用程序中执行的一项非常常见的任务,因此如果您之前就知道的话 - 但在那之前,让我们看一些代码,对吗?使用 PHP 编码 URL 参数
我必须猜测一下您的情况,但是您也可以将代码添加到您的问题(输出代码)中,以便更容易地为您提供一些您可以“仅”使用的代码。假设您有与此类似的内容来输出链接:
这将按原样输出
$category
的内容。但这确实会导致错误的 URL,从而破坏您的脚本。因此,需要对
$category
的值进行编码。这就像将其放入信封中以确保其整体到达目的地。让我们使用我刚才提到的 urlencode 函数来完成此操作:如您所见,只需将值放入 urlencode 函数即可确保数据正确编码。准备好到达目的地,没有什么可以再破坏它了。
一如既往:只要您知道如何做,事情就会很简单。 URL 需要正确编写,尤其是参数值需要正确编码,以防止在另一端(输入级别)遇到问题。
祝你任期顺利。
&
is a reserved character in HTTP URLs and URIs (commonly known as link or hypertext reference). This&
is exactly that point that will define a new$_GET
entry. So it's not part any longer of the previous$_GET
entry. The previous one is the one you would like to use. So in fact, the&
inside the category is the cause of your headaches:So just to remember: In a URL, the
&
character separates one URL parameter from the other.But this does not mean that your problem is not solve-able. You can pass values containing the
&
character via a URL parameter. There is a very simple way to get&
characters into a specific$_GET
value.Packaging Values for an Internet Round-Trip
But take care: You might look right now at the input level (that is the part where data comes into
$_GET
and other variables) of your application. But to solve this, you need to look at the opposite - the output level (that's where you send out HTML).Why? Because the output will become the input of your script. So if the output is already wrong, the input can never be ready for use.
Consider that each URL that your application sends to the browser (e.g. as part of a HTML link
<a href="">
) is a new command with it's specific parameters for your script's$_GET
variables. Technically this part is known as thequery
or as thequeryinfo
part of a HTTP URI/URL.So if the URL is already build in a way that it will create the wrong
$_GET
parameters for your script, it's already too late. So you take a look at the output to fix the originating cause.So instead, you need to look in the place you create the URL and do the correct output already there so to get the
$_GET['category']
later on.In each link-outputting place you need to ensure that every value is properly encoded. That's the magic word that contains the solution to your problem. A term you better look-up in a dictionary than in a technical documentation.
The Type of Encoding for values needed for
$_GET
parameters is the so called URL-encoding - encoding for URLs - makes sense, right? URL, encoding = URL-encoding.Encoding the values for URLs actually takes care that the parameter's value will be available in your script again safely in opposite of breaking values apart and in the end technically destroying the URL even.
Sounds probably a bit complicated, but the good news is, you first of all only need to know that PHP as a web-scripting language offers a function that does this encoding for you. It's called
urlencode()
and you can use it on any value to create your URLs. In fact it's quite a common task to do in a web-application so if you only knew earlier - but until then, let's see some code, right?Encoding URL Parameters with PHP
I must guess a bit how that looks like on your end, but you can add your code as well to your question (the output code) so that it's easier to give you some code you can "just" use. Let's say you have something similar to this to output a link:
This would output the contents of
$category
just as it is. But this does lead to wrong URLs that will break your script.So instead, the value of
$category
needs to be encoded. That is like putting it into an envelope to assure it reaches it's destination as a whole. Let's do this by using theurlencode
function I've just mentioned:As you can see, just putting the value into the
urlencode
function takes care that the data is properly encoded. Ready to reach it's destination, nothing can break it any longer.As always: It's simple when you know how. URLs need to be properly written and especially the values of the parameters need to be properly encoded to prevent running into problems at the opposite end: The input level.
Good luck with your term.
查询字符串使用
&
字符作为分隔符来分隔参数。基本上,您需要将其转义为%26
。The query string uses the
&
character as a delimiter to separate parameters. Basically you're going to need to escape it to%26
.