window.location (JS) vs header() (PHP) for redirection
Asked Answered
D

6

25

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

Derosier answered 27/3, 2013 at 9:2 Comment(5)
Javascript redirects when the document loads, PHP redirects when PHP starts parsing the PHP code, .htaccess (on Apache servers) redirects as soon as the request comes in. The sooner you redirect the better, and remember to send the right code (301 or 302 etc).Drawer
what about <meta> ? @DrawerDerosier
Avoid the meta refresh tag like the plague.Drawer
What aoid the meta tag to refresh ? @DrawerDerosier
Unless you're running a news site or something similar that with auto refresh every five minutes or so, avoid meta refresh,Drawer
R
27

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
Rectilinear answered 27/3, 2013 at 9:4 Comment(0)
B
14

A better way to set the location in JS is via:

window.location.href = 'https://stackoverflow.com';

Whether to use PHP or JS to manage the redirection depends on what your code is doing and how. But if you're in a position to use PHP; that is, if you're going to be using PHP to send some JS code back to the browser that simply tells the browser to go somewhere else, then logic suggests that you should cut out the middle man and tell the browser directly via PHP.

Buonaparte answered 27/3, 2013 at 9:9 Comment(0)
A
8

It depends on how and when you want to redirect the user to another page.

If you want to instantly redirect a user to another page without him seeing anything of a site in between, you should use the PHP header redirect method.

If you have a Javascript and some action of the user has to result in him entering another page, that is when you should use window.location.

The meta tag refresh is often used on download sites whenever you see these "Your download should start automatically" messages. You can let the user load a page, wait for a certain amount of time, then redirect him (e.g. to a to-be-downloaded file) without Javascript.

Adelaideadelaja answered 27/3, 2013 at 9:18 Comment(0)
B
3

PHP redirects are better if you can as with the JavaScript one you're causing the client to load the page before the redirect, whereas with the PHP one it sends the proper header.

However the PHP shouldn't go in the <head>, it should go before any output is sent to the client, as to do otherwise will cause errors.

Using <meta> tags have the same issue as Javascript in causing the initial page to load before doing the redirect. Server-side redirects are almost always better, if you can use them.

Bacciferous answered 27/3, 2013 at 9:6 Comment(2)
But I pud JS in <head> and I using end(); (PHP) after thatDerosier
Really you should do as much of your PHP work as possible before sending any output to the browser. If you try and do stuff that involves headers (like redirects) after sending output then it will cause a "cannot modify header information" error.Bacciferous
U
2

The first case will fail when JS is off. It's also a little bit slower since JS must be parsed first (DOM must be loaded). However JS is safer since the destination doesn't know the referer and your redirect might be tracked (referers aren't reliable in general yet this is something).

You can also use meta refresh tag. It also requires DOM to be loaded.

Usurer answered 27/3, 2013 at 9:5 Comment(0)
S
0
window.location.href = 'url';

is beter than

header('location:url');

because the header command is mustly return an error "Warning: Cannot modify header information - headers already sent"

using js window.location.href = 'url';

this is beter

Scoutmaster answered 10/8, 2022 at 1:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.