简体   繁体   English

header()重定向后如何使用变量?

[英]How do I use variables after header() redirect?

I've spent ages trying to figure this out after reading guide after guide and I can't get it. 我花了很长时间试图在逐一阅读指南之后弄清楚这一点,但我无法理解。 I have 3 files - user.php, map.php and newaddress.php. 我有3个文件-user.php,map.php和newaddress.php。 I've only included what I understand to be the important bits to this issue. 我只介绍了我认为是此问题的重要内容。 Happy to provide more info if needed. 如果需要,很高兴提供更多信息。

user.php passes a "mapnumber" to map.php. user.php将“ mapnumber”传递给map.php。

user.php user.php

<form action="map.php" method="post">
   <input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
   <input type="hidden" name="process" value="process"/>
   <button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>

map.php receives "mapnumber" and generates a list of addresses from the database with matching "mapnumber". map.php接收“ mapnumber”,并从数据库中生成匹配“ mapnumber”的地址列表。 From map.php, user can add new addresses which take the "mapnumber" value and is processed in newaddress.php. 用户可以从map.php添加带有“ mapnumber”值并在newaddress.php中处理的新地址。

map.php map.php

$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>

newaddress.php adds the address to database with the "mapnumber" value and then redirects back to map.php where it SHOULD generate the list of addresses based on "mapnumber" but map.php doesn't pick up the "mapnumber" from newaddress.php and hence the list of addresses isn't generated. newaddress.php使用“ mapnumber”值将地址添加到数据库中,然后重定向回map.php,在其中应基于“ mapnumber”生成地址列表,但map.php不会从newaddress中获取“ mapnumber” .php,因此不会生成地址列表。

newaddress.php newaddress.php

$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php");
exit;

Please help 请帮忙

You can set the get variable like this: 您可以这样设置get变量:

$mapnumber = $mysqli->escape_string($_POST['mapnumber']); header("Location: map.php?nr=". $mapnumber); exit;

Than in map.php you get get it by: $mapnumber = $mysqli->escape_string($_GET['nr']); 比在map.php您可以通过以下方式获得它: $mapnumber = $mysqli->escape_string($_GET['nr']);

You could use cookies to store values in the remote browser. 您可以使用cookie将值存储在远程浏览器中。 But the real problem you encounter is that the Location redirect is just that. 但是您遇到的真正问题是位置重定向就是这样。 It tells the browser to load another URL. 它告诉浏览器加载另一个URL。 You can add get requests to that Location redirect URL like that: 您可以将get请求添加到该位置重定向URL,如下所示:

header("Location: map.php?mapnumber=".urlencode($mapnumber));

But please keep in mind that the default Location redirect will be a permanent one (HTTP Status Code 301) and it is up to the browser to recheck if the redirecting url/document might have changed or not. 但是请记住,默认的位置重定向将是永久的(HTTP状态码301),并且由浏览器重新检查重定向的URL /文档是否可能已更改。

http://php.net/manual/en/function.header.php#78470 http://php.net/manual/zh/function.header.php#78470

So you might want to consider using a 307 redirect: 因此,您可能需要考虑使用307重定向:

header("Location: map.php?mapnumber=".urlencode($mapnumber), TRUE,307);

Anyway: passing variables from one page to another is generally not a good practice. 无论如何:将变量从一页传递到另一页通常不是一个好习惯。 Try to avoid it and try to dig into sessions. 尝试避免这种情况,并尝试深入探讨会话。

http://php.net/manual/en/intro.session.php http://php.net/manual/zh/intro.session.php

I recommend you to use get method in map.php 我建议您在map.php中使用get方法
Then it will receive map number like map.php?mapnumber=somenum 然后它将收到地图编​​号,例如map.php?mapnumber=somenum

user.php user.php

<form action="map.php" method="get">
   <input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
   <input type="hidden" name="process" value="process"/>
   <button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>

map.php map.php

$mapnumber = $mysqli->escape_string($_GET['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>

newaddress.php newaddress.php

$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?mapnumber=$_POST['mapnumber']");
exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM