PHP 传递带有标头重定向的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10048721/
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 passing a variable with header redirect
提问by somdow
I am trying to pass a variable within a header link. I have tried below code
我试图在标题链接中传递一个变量。我试过下面的代码
$name = "mike";
if($name != "lopan"){
header("Location:error-page.php?$errorMssg=Please enter information?");
}
This page redirect to location but doesn't pass the variable that contains the message. But when i create a simple link with values like this:
此页面重定向到位置,但不传递包含消息的变量。但是当我用这样的值创建一个简单的链接时:
<a href="error-page.php?$errorMssg=so what happened there buddy?">link</a>
it passes it just fine.
它通过它就好了。
Any ideas what i am doing wrong? or i can not pass information with headers?
任何想法我做错了什么?或者我不能通过标题传递信息?
回答by Benjamin Crouzier
You need to use urlencodelike this:
您需要像这样使用urlencode:
if($name != "lopan"){
header("Location:error-page.php?errorMssg=".urlencode("Waoo so your not EVEN going to try to enter information huh?"));
}
And in error-page.php, you should get it (no need to urldecode):
在 error-page.php 中,你应该得到它(不需要 urldecode):
<?php
$errorMssg = $_GET['errorMssg'];
回答by kontur
Remove the $before errorMssgand urlencodethe message.
删除$之前errorMssg和urlencode消息。
回答by mkk
could it be because you have $errorMssginstead of $errorMsg? also try to make a valid url, for example replace " " with %20 etc, function urlencode()could help you with this.
可能是因为你有$errorMssg而不是$errorMsg?还尝试创建一个有效的 url,例如用 %20 等替换“”,函数urlencode()可以帮助你解决这个问题。

