window.location (JS) vs header() (PHP) for redirection

javascript, meta, php, redirect

Solution

The result is same for all options. Redirect.

`<meta>` in HTML:

- Show content of your site, and next redirect user after a few (or 0) seconds.

- Don't need JavaScript enabled.

- Don't need PHP.

`window.location` in JS:

- Javascript enabled needed.

- Don't need PHP.

- Show content of your site, and next redirect user after a few (or 0) seconds.

- Redirect can be dependent on any conditions `if (1 === 1) { window.location.href = 'http://example.com'; }`.

`header('Location:')` in PHP:

- Don't need JavaScript enabled.

- PHP needed.

- Redirect will be executed first, user never see what is after. `header()` must be the first command in php script, before output any other. If you try output some before header, will receive an `Warning: Cannot modify header information - headers already sent`

Problem

using JS : (in `<head>` tag) ``` <script>window.location="https://stackoverflow.com";</script> ``` using PHP : (in `<head>` tag) ``` header('Location: https://stackoverflow.com'); end(); ``` Which one I should use ? or another ? and what about using `<meta>`? ``` <meta http-equiv="refresh" content="0;url=https://stackoverflow.com"/> ``` Many good answers , I don't know which answer I will accept, Thanks so much

Original source