简体   繁体   中英

Using PHP in a header to redirect a webpage

I am using php to redirect a php file:

 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?

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).

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 )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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