如何让 PHP 忽略查询字符串中未转义的 & 符号(例如 ?name=M&M's 不会成为数组('name' => 'M' ;, 'M\'s'))

发布于 2024-10-07 00:02:41 字数 626 浏览 0 评论 0原文

URL 如下: http://any.php?name=M&M's

PHP 的 $_GET 变量是 array('name' => 'M ', 'M\s' => '')

有没有办法让 PHP 忽略那个未转义的 & 符号?

------ 之前的问题(导致上面的问题) ------

大家好,

有了这样的 mod_rewrite 规则...

RewriteRule ^wiki/([A -Za-z0-9_,+&'\-\)\(]+)$ php/data.php?name=$1 [L,NC]

和这样的链接(纯文本形式,即M&M's)...

http://any.com/wiki/M%26M%27s

我从 data.php 得到这个...

名称:M

名称的其余部分发生了什么?

谢谢!

With a URL like: http://any.php?name=M&M's

PHP's $_GET variable is array('name' => 'M', 'M\s' => '').

Is there a way to get PHP to ignore that un-escaped ampersand?

------ Earlier question (that lead to one above) ------

Hi guys,

With a mod_rewrite rule like this...

RewriteRule ^wiki/([A-Za-z0-9_,+&'\-\)\(]+)$ php/data.php?name=$1 [L,NC]

And a link like this (in plain text, that's M&M's)...

http://any.com/wiki/M%26M%27s

I'm getting this from data.php...

<?php echo 'Name:'. $_GET['name']; ?>

Name: M

What happened to the rest of the name?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀お生予夺 2024-10-14 00:02:41

我不知道这是否有帮助,但是您确定需要在范围表达式中使用反斜杠吗?通常,您不需要在那里转义括号(它们永远不会合法),并且您也不需要转义连字符:您将其包含在第一个或最后一个位置。所以你的行将是:

RewriteRule ^wiki/([-A-Za-z0-9_,+&')(]+)$ php/data.php?name=$1 [L,NC]

而且,再说一遍,我不知道 mod_rewrite,所以我只是在黑暗中进行了两次尝试,但是:

  1. 正则表达式的“php”部分之前的 $ 是正则表达式的一部分,还是某种变量引用?有混乱吗?
  2. 您确定该规则应该在 URL 的反转义版本上运行吗?如果传递的字符串中包含 %,那么您的正则表达式中应该包含 %...不是吗?

I don't know if this helps, but are you sure you need the backslashes in the range expression? Normally you don't need to escape parentheses there (they would never be legal), and you don't escape the hyphen either: you include it either in first or last position. So your line would be:

RewriteRule ^wiki/([-A-Za-z0-9_,+&')(]+)$ php/data.php?name=$1 [L,NC]

And, again, I don't know mod_rewrite, so I'm just taking two stabs in the dark here but:

  1. Is the $ just before "php" part of the regex, or some sort of variable reference? Is there confusion?
  2. Are you sure this rule should be operating on the de-escaped version of the URL? If it's being passed a string with %'s in it, then your regex should have %'s in it...no?
丢了幸福的猪 2024-10-14 00:02:41

所以我又做了一点测试,看来是 PHP 的错!

首先,mod_rewrite 在运行重写规则之前似乎会解码任何字符。如果我从正则表达式的字符类中删除“$”和“'”(或按照 Steve 建议添加“%”),则会收到 404 错误。因此,我的 RewriteRule 语法和 mod_rewrite 看起来越来越好(特别是在像 Steve 建议的那样清理之后)。

如果我转储页面的 $_GET 变量,我会看到:

<?php var_dump($_GET); ?>
array(2) { ["name"]=> string(8) "Almond_M" ["M\'s"]=> string(0) "" } 

哈!这是一个 PHP 问题!所以现在的问题是,如何让 PHP 忽略未转义的 & 符号(我想保持它不转义,这样对用户来说看起来很漂亮)。

So I did a little more testing, and it looks like PHP's fault!

First of all, it appears mod_rewrite decodes any characters before running through the re-write rules. If I remove the "$" and "'" from my regex's character class (or add the "%" as Steve suggested), I get a 404 error. Thus, my RewriteRule's syntax and mod_rewrite looks better and better (especially after cleaning it up like Steve suggested).

If I dump the page's $_GET variable, I see:

<?php var_dump($_GET); ?>
array(2) { ["name"]=> string(8) "Almond_M" ["M\'s"]=> string(0) "" } 

Ha! It's a PHP problem! So now the question is, how do I get PHP to ignore the unescaped ampersand (and I'd like to keep it un-escaped so it looks pretty for users).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文