1

Possible Duplicate: How to make a redirect in PHP?


Hi! How do i forward a page on the best way? Should I use the header-funct. or should i use HTML (meta-tags) to refresh? I hope some experts could give me some advice at this point. Thanks!

Btw, the forwarding is made inside an if-statement if that could be to some problem?

1

2 Answers 2

6

If you want to redirect the user to an URL, you can use the header function to send a Location HTTP header :

header('Location: http://www.example.com/new-url.php');
die;

(In theory, you should use an absolute URL that includes the domain name -- but most browsers accept a non-absolute URL)

You can use this wherever you want in your script, even inside a if-block, of course.

The only thing is, as you are setting an HTTP-header : you must not have sent any kind of output before (not even a white space at the end of an included file).

3
  • Okay, thanks! So I could do this to add variable to URL: header('Location: example.com/new-url.php?ref='. $ref .''); Dec 17, 2009 at 17:41
  • Also be careful when you pass variables, one bad example: header('Location: example.com/$_GET["url"].php')
    – Jay Zeng
    Dec 17, 2009 at 17:42
  • Yes, you can add variables to the URL : you just have to build the URL you want to redirect to ;-) Dec 17, 2009 at 18:41
0

You can use JavaScript.

echo "<script>location.replace(\"$url\");</script>";

Take a look at https://www.w3schools.com/howto/howto_js_redirect_webpage.asp .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.