Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 645
l2aelba Avatar asked Mar 27 '13 09:03

l2aelba


People also ask

Which PHP method can be used to redirect a Web page onto a next location?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php .

What is window location in PHP?

Using JavaScript via PHP: The windows. location object in JavaScript is used to get the current page address(URL) and to redirect the browser to a new page. The window. location object contains the crucial information about a page such as hostname, href, pathname, port etc.

How do I automatically redirect in PHP?

To set a permanent PHP redirect, you can use the status code 301. Because this code indicates an indefinite redirection, the browser automatically redirects the user using the old URL to the new page address.

What is the use of header location in PHP?

The header() function is an inbuilt function in PHP which is used to send a raw HTTP header. The HTTP functions are those functions which manipulate information sent to the client or browser by the Web server, before any other output has been sent.


2 Answers

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
like image 187
Damonsson Avatar answered Sep 28 '22 10:09

Damonsson


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.

like image 21
Wintermute Avatar answered Sep 28 '22 09:09

Wintermute