在页眉中使用PHP重定向网页


Using PHP in a header to redirect a webpage

我正在使用php重定向php文件:

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

问题是php没有被视为php,而是从字面上理解的,我如何才能在这个标题中回显变量?

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

试试这个:

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

1) 注意Location中的大写字母"L",尽管http头应该是不区分大小写的(按照规范),但一些浏览器将其视为区分大小写(IE)。

2) 如果可能的话,你不应该使用相对路径,因为如果浏览器在url中留下了一个斜杠,你可能会被重定向到错误的页面(即。http://domain.com/folder/将重定向到http://domain.com/folder/viewprofile.php?id=5)

无需回声。只需使用以下代码:

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

变量$id中存储的值将附加到头字符串中。