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

172,242

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
172,242

Related videos on Youtube

l2aelba
Author by

l2aelba

https://paypal.me/l2aelba

Updated on July 09, 2022

Comments

  • l2aelba
    l2aelba almost 2 years

    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

    • adeneo
      adeneo about 11 years
      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).
    • l2aelba
      l2aelba about 11 years
      what about <meta> ? @adeneo
    • adeneo
      adeneo about 11 years
      Avoid the meta refresh tag like the plague.
    • l2aelba
      l2aelba about 11 years
      What aoid the meta tag to refresh ? @adeneo
    • adeneo
      adeneo about 11 years
      Unless you're running a news site or something similar that with auto refresh every five minutes or so, avoid meta refresh,
  • l2aelba
    l2aelba about 11 years
    But I pud JS in <head> and I using end(); (PHP) after that
  • krisnoble
    krisnoble about 11 years
    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.