php 如何在php中使用通过header()传递的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13266294/
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 use variables passed through header() in php
提问by Abdul Hadi Shakir
I'm using header()to pass var userfrom one page to another as:
我header()用来将 varuser从一个页面传递到另一个页面:
header( "Location: temp.php? user = $user" );
the variable is getting passed and is shown on the url of another page.
该变量正在传递并显示在另一个页面的 url 上。
But i dont know how to use these var userin that page. Please help.
但我不知道如何user在该页面中使用这些 var 。请帮忙。
回答by Naryl
if the 'other page' is in PHP, you only need to:
如果“其他页面”在 PHP 中,您只需要:
$user=$_GET['user'];
EDIT: If you are not sure if you will receive 'user' and want to avoid error messages you should do:
编辑:如果您不确定是否会收到“用户”并希望避免错误消息,您应该这样做:
if(isset($_GET['user'])){
$user=$_GET['user'];
}else{
//user was not passed, so print a error or just exit(0);
}
回答by Chris
page1.php
页面1.php
<?php
$user = "batman";
header("Location:temp.php?user=".$user);
exit();
?>
temp.php?user=batman (you have just been redirected here)
temp.php?user=batman(你刚刚被重定向到这里)
<?php
if($_GET){
echo $_GET['user'] // print_r($_GET);
}else{
echo "Url has no user";
}
?>
Or you could use a $_SESSION - but this could easily complicate things
或者您可以使用 $_SESSION - 但这很容易使事情复杂化
page1.php
页面1.php
<?php
session_start();
$_SESSION['user'] = "batman";
header("Location:temp.php);
exit();
?>
temp.php
临时文件
<?php
session_start();
echo $_SESSION['user'];
unset($_SESSION['user']); // remove it now we have used it
?>
回答by Deepu
if you are using this to pass value
如果您使用它来传递值
header( "Location:temp.php? user = $user" );
then on temp.php you have to use
然后在 temp.php 上你必须使用
$var=$_GET['user'];
to get the value and now $var contains the value you passed.
获取值,现在 $var 包含您传递的值。

