&功放;&QUOT与urlen code和&QUOT PHP $ _GET变种;错误 [英] PHP $_GET var with urlencode and "&" bug

查看:154
本文介绍了&功放;&QUOT与urlen code和&QUOT PHP $ _GET变种;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code,我创建了一个链接是这样的:

In my code, I create a link like this:

$link = 'http://www.mydomain.com/'.urlencode($str).'/1';

我用URL重写,在我的htaccess文件的规则是这样的:

I use url-rewriting and the rule in my htaccess file looks like this:

rewriteRule ^(.+)/(.*)$ index.php?var1=$1&var2=$2 [NC,L]

这code工作正常,几乎每一个字符串。但有时,字符串连接code包含&放大器;。该urlen code函数连接codeS它corectly,但是当我读到$ _GET数组在PHP中,它看起来像这样(用$海峡='substring1和放大器; substring2')

This code is working fine for almost every strings. But sometimes, the string to encode contains "&". The urlencode function encodes it corectly, but when I read the $_GET array in php, it looks like this (with $str = 'substring1&substring2'):

'var1' => 'substring1' (without "&")
'substring2' => '' (without "&")
'var2' => 1

我真正需要的&放大器;在我的变种。有没有一种方法来连接code这个角色,使其工作?

I really need the "&" in my var. Is there a way to encode that character to make it works?

另外,我真的不知道为什么,但有时我得到一个禁止HTTP错误与通过为var1的一些字符串。显然,他们有没有什么特别的,因为为例,Décarie广场,使该错误。其他字符串用空格和E工作正常。

Also, I really don't know why, but sometimes I get a forbidden http error with some strings passed as var1. Apparently, they have nothing special, for exemple, "Décarie Square" makes that error. Other strings with spaces and "é" are working fine.

推荐答案

Apache的mod_rewrite自动去codeS urlen codeD时,它的正则表达式匹配的字符串。但它只做这一次,所以你应该,如果你urlen code的字符串两次。这将重新逃避所有的'%'字符。

Apache's mod_rewrite automatically decodes urlencoded strings when it does regex matching. But it only does this once, so you should be if you urlencode your string twice. This will re-escape all of those `%' characters.

尝试

$link = 'http://www.mydomain.com/'.urlencode(urlencode($str)).'/1';

或停止依靠重写规则,并使用该处理的URL路径选择正确的框架。

or stop relying on rewrite rules and use a framework that handles URL routing properly.

哦,而且还应该有ヶ辆()地方在那里。

Oh, and there should also be htmlentities() somewhere in there.

这篇关于&功放;&QUOT与urlen code和&QUOT PHP $ _GET变种;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆