标头函数中的 PHP 变量
我尝试使用标头函数通过 URL 传递变量作为重定向页面的方式。但是,当页面重定向时,它会传递实际的变量名称,而不是与变量关联的值。我是 PHP 新手,不完全理解语法,因此任何有关正确方法的进一步解释将不胜感激。
header('location: index.php?id=".$_POST[ac_id]."&err=".$login."');
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.
header('location: index.php?id=".$_POST[ac_id]."&err=".$login."');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您想要:
您在此字符串中组合了
'
和"
,这就是它无法正确插入变量的原因。在上面的示例中,您严格打开字符串与"
并将变量与字符串连接起来。You want:
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.引号内有引号。试试这个:
urlencode() 函数负责处理网址。
如果您认为 URL 中会有不止一两个变量,我会使用
http_build_query()
。此外,从技术上讲,您不能在位置标头中使用相对路径。虽然它适用于大多数浏览器,但根据 RFC 无效。您应该包含完整的 URL。
You have quotes within quotes. Try this instead:
The urlencode() function takes care of any reserved characters in the 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.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.
尝试会话存储。
header 用于重定向页面。
如果你真的想传递价值观
只有通过 header 你才能生成 url。
header('位置:destination.php?
值1=1&值2=3');
但这对于 var 来说不是一个好的做法。
只需将值存储在 SESSION 全局变量中。 B4 header() 重定向调用。
@您必须测试的接收页面,如果会话 val isset() n !empty() 那么...否则...
希望这会有所帮助。
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 ...
Hope this wil help.
就我而言,很多时候 header() 无法正常工作。我使用 window.location.href 代替标头函数。
你可以这样尝试,
In my case, many times header() not working properly. Instead of header function, I use window.location.href.
You can try like this,
试试这个:
PHP 变量在双引号字符串内展开。
Try this:
PHP variables are expanded inside double-quoted strings.