简体   繁体   English

在标头中使用PHP重定向网页

[英]Using PHP in a header to redirect a webpage

I am using php to redirect a php file: 我正在使用php重定向php文件:

 header("location:viewprofile.php?id=<?php echo $id; ?>");

The problem is the php is not taken as php and is taken literally, how can I echo the variable in this header? 问题是php不像php那样直接取而代之,我如何在此标头中回显变量?

header("location:viewprofile.php?id=$id");

try this : 尝试这个 :

header("Location: viewprofile.php?id={$id}");

1) notice the capital "L" in Location, although http headers should be case-insensitive (as by the specifications), some browsers treat them as case-sensitive (IE). 1)注意位置中的大写字母“ L”,尽管http标头应不区分大小写(根据规范),但某些浏览器会将它们视为区分大小写(IE)。

2) you should not use relative paths if possible, because if a trailing slash is left by the browser in the url, you might be redirected to the wrong page (ie http://domain.com/folder/ will redirect to http://domain.com/folder/viewprofile.php?id=5 ) 2)如果可能,您不应使用相对路径,因为如果浏览器在URL中留下了斜杠,您可能会被重定向到错误的页面(即http://domain.com/folder/将重定向到http: //domain.com/folder/viewprofile.php?id=5

No need to echo it. 无需回显它。 just use the following code: 只需使用以下代码:

header("location:viewprofile.php?id=".$id);

the stored value in the variable $id will be attached to the header string. 变量$ id中存储的值将附加到标题字符串。

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

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