-1

I try this to redirect page using PHP:

if($var="error") echo '<script>location.href="404.php"</script>';

Works, but, is possible redirect the page directly from PHP?

Code Slicer
  • 542
  • 6
  • 13
  • should have been a very simple web search `php redirect`. WOuld have found it faster than it took to sign up on this site – charlietfl Nov 02 '13 at 19:15
  • Hi, welcome to Stack Overflow! Please always search before asking. A search for `redirect using php` would have given you the correct answer. – Amal Murali Nov 02 '13 at 19:15

5 Answers5

0

Yes, it is.

header('Location: http://google.com');
exit();
Wiggler Jtag
  • 669
  • 8
  • 23
  • +1 for [exit after header](http://stackoverflow.com/questions/2747791/why-i-have-to-call-exit-after-redirection-through-headerlocation-in-php) – Dave Chen Nov 02 '13 at 19:46
-1

PHP redirects are done with header()

header('Location : '404.php');

DOCS

charlietfl
  • 170,828
  • 13
  • 121
  • 150
-1

It is possible if you do it before any content:

<?php header("Location: 404.php"); ?>

If you have sent any content to the client, however, it will not work. This means even a doctype or whitespaces before the first PHP block.

Also, if you want to stop processing that page there just add exit; or exit(); there.

PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
-1

Yes... you can use the location headers. Example:

<?php header('Location: 404.php'); ?>

However you can not use this if there is any kind of output before this statement.

SpiderLinked
  • 363
  • 1
  • 6
  • 14
-1

Use header() function

header("Location: 404.php");
exit();
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82