PHP通过头重定向传递变量

时间:2022-07-09 15:21:32

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?

你知道我做错了什么吗?或者我不能用标题传递信息?

3 个解决方案

#1


11  

You need to use urlencode like 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):

和页面偷走了。php,你应该得到它(不需要urldecode):

<?php
$errorMssg = $_GET['errorMssg'];

#2


5  

Remove the $ before errorMssg and urlencode the message.

删除errorMssg之前的$并对消息进行urlencode。

#3


1  

could it be because you have $errorMssg instead 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()可以帮助您实现这一点。

#1


11  

You need to use urlencode like 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):

和页面偷走了。php,你应该得到它(不需要urldecode):

<?php
$errorMssg = $_GET['errorMssg'];

#2


5  

Remove the $ before errorMssg and urlencode the message.

删除errorMssg之前的$并对消息进行urlencode。

#3


1  

could it be because you have $errorMssg instead 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()可以帮助您实现这一点。