PHP URL 编码/解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5662249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP URL Encoding / Decoding
提问by Imran Omar Bukhsh
I used the solution accepted for this questionfor encrypting by id for example in /index.php?id=3. The problem is I cannot send the encrypted value as an url, example /index.php?id=dsf13f3343f23/23=. Because sometimes it will have weird characters in the url e.g. notice the =
sign in the end
我使用此问题接受的解决方案通过 id 加密,例如在/index.php?id=3。问题是我无法将加密值作为 url 发送,例如/index.php?id=dsf13f3343f23/23=。因为有时它会在 url 中有奇怪的字符,例如注意最后的=
符号
回答by Pascal MARTIN
The weird charactersin the values passed in the URL should be escaped, using urlencode(
).
URL 中传递的值中的奇怪字符应该使用urlencode(
)进行转义。
For example, the following portion of code :
例如,以下代码部分:
echo urlencode('dsf13f3343f23/23=');
would give you :
会给你:
dsf13f3343f23%2F23%3D
Which works fine, as an URL parameter.
作为 URL 参数,它工作正常。
And if you want to build aquery string with several parameters, take a look at the http_build_query()
function.
如果您想构建带有多个参数的查询字符串,请查看该http_build_query()
函数。
For example :
例如 :
echo http_build_query(array(
'id' => 'dsf13f3343f23/23=',
'a' => 'plop',
'b' => '$^@test',
));
will give you :
会给你 :
id=dsf13f3343f23%2F23%3D&a=plop&b=%24%5E%40test
This function deals with escaping and concatenating the parameters itself ;-)
此函数处理转义和连接参数本身;-)
回答by Lucas Jones
Use PHP's urlencode()
function to encode the value before you put it into a URL.
urlencode()
在将值放入 URL 之前,使用 PHP 的函数对值进行编码。
string
urlencode
( string
$str
)
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.
string
urlencode
( string
$str
)
在对 URL 的查询部分中使用的字符串进行编码时,此函数很方便,可以方便地将变量传递到下一页。
This function converts "weird" characters, such as =
, into a format safe to put into a URL. You can use it like this:
此函数将“怪异”字符(例如=
)转换为可安全放入 URL 的格式。你可以这样使用它:
Header('Location: /index.php?id=' . urlencode($id))
回答by Gumbo
If you use Base64 to encode the binary value for the URL, there is also a variant with URL and filename safe alphabet.
如果您使用 Base64 对 URL 的二进制值进行编码,则还有一个带有 URL 和文件名安全字母表的变体。
You can use the strtr
functionto translate one from alphabet to the other:
您可以使用该strtr
函数将一个字母从字母转换为另一个:
$base64url = strtr($base64, '+/', '-_');
$base64 = strtr($base64url, '-_', '+/');
So you can use these functions to encode and decode base64url:
所以你可以使用这些函数对base64url进行编码和解码:
function base64url_encode($str) {
return strtr(base64_encode($str), '+/', '-_'));
}
function base64url_decode($base64url) {
return base64_decode(strtr($base64url, '-_', '+/'));
}
See also my answer on What is a good way to produce an short alphanumeric string from a long md5 hash?
回答by Your Common Sense
There is no use in encrypting parameters.
Send it as is:
加密参数没有用。
按原样发送:
/index.php?id=3
nothing wrong with it.
没有错。