php 如何将电子邮件值传递给 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20559555/
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
How to pass email value to url
提问by user3097168
Hi I'm here again with the same question
嗨,我又来了,同样的问题
<?php
$email="[email protected]";
$decode=urldecode($email);
?>
<FORM METHOD="get" ACTION="action1.php">
Type your first name: <INPUT TYPE="text" NAME="FirstName"> <br/>
Type your last name: <INPUT TYPE="text" NAME="LastName"> <br/>
Type your email:<INPUT type="hidden" NAME="email" value="<?php $decode; ?>">
<INPUT TYPE="submit" VALUE="Click and See">
</FORM>
I got the url redirect: http://example.com/action1.php?FirstName=jon&LastName=Kuri&email=xyz%40abc.com
我得到了 url 重定向:http: //example.com/action1.php?FirstName=jon& LastName=Kuri&email= xyz%40abc.com
but I need it to be http://example.com/action1.php?FirstName=jon&LastName=Kuri&[email protected]
但我需要它是http://example.com/action1.php?FirstName=jon&LastName=Kuri&[email protected]
cause email is used here for username. So saying this wrong.
因为这里使用电子邮件作为用户名。所以说错了。
Thanks for your help in advance.
提前感谢您的帮助。
回答by Brad
It's not wrong... you're wrong. Reserved characters in URLs must be encoded to be valid, or they are ambiguous and might be used for their reserved purposes in the URL. @is a reserved character, and must be encoded as %40.
这没有错……你错了。URL 中的保留字符必须经过编码才能有效,否则它们是不明确的并且可能用于 URL 中的保留目的。 @是保留字符,必须编码为%40.
PHP decodes this for you. It's transparent to you, and you don't even have to worry about it.
PHP 为您解码。它对您是透明的,您甚至不必担心。
Here is a reference for you: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
这是给你的参考:http: //www.blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
回答by Basit
Why are you even decoding the $emailin the first place. To pass a string into URL, it is very much advisable to encode them so the special characters don't mess with reserved characters like @. In PHP, encoding and decoding a URL is as simple as calling urlencode()and urldecode()respectively.
Since you want to pass the email to a URL, you should be encoding it instead of decoding.
I've modified your code to:
为什么你甚至首先解码$email。要将字符串传递到 URL,建议对它们进行编码,这样特殊字符就不会与@. 在 PHP 中,对 URL 进行编码和解码就像分别调用urlencode()和一样简单urldecode()。由于您想将电子邮件传递给 URL,您应该对其进行编码而不是解码。我已将您的代码修改为:
<?php
$email="[email protected]";
$encodedEmail=urlencode($email);
?>
<FORM METHOD="get" ACTION="action1.php">
Type your first name: <INPUT TYPE="text" NAME="FirstName"> <br/>
Type your last name: <INPUT TYPE="text" NAME="LastName"> <br/>
Type your email:<INPUT type="hidden" NAME="email" value="<?php $encodedEmail; ?>">
<INPUT TYPE="submit" VALUE="Click and See">
</FORM>
Now, whenever you have to use the email address from the URL, simply do the following:
现在,每当您必须使用 URL 中的电子邮件地址时,只需执行以下操作:
$decodedEmail = urldecode($_GET['email']); // It will return [email protected] as a string, decoded.
Hope it answers your question.
希望它能回答你的问题。

