php 头函数中的PHP变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5785304/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:29:25  来源:igfitidea点击:

PHP variable in header function

phpsyntaxurlvariables

提问by Joe

I am attempting to pass variables through the URL using the header function as a way to redirect the page. But when the page is redirected it passes the actual variable names rather than the values associated with the variables. I am new to PHP and do not fully understand the syntax so any further explanation as to the proper way to do this would be much appreciated.

我正在尝试使用标头函数通过 URL 传递变量作为重定向页面的一种方式。但是当页面被重定向时,它传递的是实际的变量名称,而不是与变量关联的值。我是 PHP 的新手并且不完全理解语法,所以任何关于正确方法的进一步解释将不胜感激。

header('location: index.php?id=".$_POST[ac_id]."&err=".$login."');

回答by Mike Lewis

You want:

你要:

header("Location: index.php?id=".$_POST['ac_id']."&err=".$login);

You were combining 'and "in this string, which is why it couldn't interpolate the variables properly. In my example above, you are strictly opening the string with "and concatenating the variables with the string.

您在此字符串中组合了'and ",这就是它无法正确插入变量的原因。在我上面的示例中,您严格打开字符串"并使用字符串连接变量。

回答by Brad

You have quotes within quotes. Try this instead:

你有引号内的引号。试试这个:

header('location: index.php?id=' . urlencode($_POST['ac_id']) . '&err=' . urlencode($login));

The urlencode()function takes care of any reserved characters in the url.

进行urlencode()函数负责的任何保留字符在URL中。

What I would do instead is use http_build_query(), if you think you will have more than one or two variables in the URL.

我会做的是使用http_build_query(),如果您认为 URL 中将有超过一两个变量。

header('Location: index.php?' . http_build_query(array(
    'id' => $_POST['ac_id'],
    'err' => $login
)));

Also, you technically can't use relative paths in the location header. While it does work with most browsers, it is not valid according to the RFCs. You should include the full URL.

此外,从技术上讲,您不能在位置标头中使用相对路径。虽然它适用于大多数浏览器,但根据 RFC,它无效。您应该包含完整的 URL。

回答by Junaid

Try SESSION storage. header is use to redirect the page. and if you really want to pass values through header only then u have genrate url. header('location:destination.php? value1=1&value2=3'); but it is not a good practice for vars. just store values in SESSION global var. B4 the header() redirection call. @the recieve page u have to test, if the session val isset() n !empty() then ... or else ...

尝试会话存储。header 用于重定向页面。如果您真的只想通过标头传递值,那么您就有了生成网址。header('location:destination.php?value1=1&value2=3'); 但这对 vars 来说不是一个好习惯。只需将值存储在 SESSION 全局变量中。B4 header() 重定向调用。@the recieve page 你必须测试,如果 session val isset() n !empty() 那么 ... 否则 ...

Hope this wil help.

希望这会有所帮助。

回答by Rushit

You can try this

你可以试试这个

header("Location:abc.html?id=".$_POST['id']."id_2=".$var['id_2']);

header("Location:abc.html?id=".$_POST['id']."id_2=".$var['id_2']);

If it works let me know. This is just an example.

如果有效,请告诉我。这只是一个例子。

回答by Stephen

Try this:

尝试这个:

header("location: index.php?id=$_POST[ac_id]&err=$login");

PHP variables are expanded inside double-quoted strings.

PHP 变量在双引号字符串中展开。

回答by LanceHub

header('location: index.php?id='.$_POST['ac_id'].'&err='.$login);