user3505931
user3505931

Reputation: 269

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?

Upvotes: 0

Views: 57

Answers (3)

redmoon7777
redmoon7777

Reputation: 4526

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 (i.e. http://domain.com/folder/ will redirect to http://domain.com/folder/viewprofile.php?id=5)

Upvotes: 1

DoubleM
DoubleM

Reputation: 464

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.

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 77986

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

Upvotes: 4

Related Questions