window.location (JS) vs header() (PHP) 用于重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15655017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:41:45  来源:igfitidea点击:

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

phpjavascriptredirectmeta

提问by l2aelba

using JS :(in <head>tag)

使用 JS :(<head>标签中)

<script>window.location="https://stackoverflow.com";</script>

using PHP :(in <head>tag)

使用 PHP :(在<head>标签中)

header('Location: https://stackoverflow.com');
end();

Which one I should use ? or another ?

我应该使用哪一种?或其他 ?

and what about using<meta>?

关于使用什么<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

很多好的答案,我不知道我会接受哪个答案,非常感谢

回答by Damonsson

The result is same for all options. Redirect.

所有选项的结果都相同。重定向。

<meta>in HTML:

<meta>在 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.
  • 显示您网站的内容,并在几秒(或 0)秒后重定向用户。
  • 不需要启用 JavaScript。
  • 不需要PHP。

window.locationin JS:

window.location在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'; }.
  • 需要启用 Javascript。
  • 不需要PHP。
  • 显示您网站的内容,并在几秒(或 0)秒后重定向用户。
  • 重定向可以依赖于任何条件if (1 === 1) { window.location.href = 'http://example.com'; }

header('Location:')in PHP:

header('Location:')在 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
  • 不需要启用 JavaScript。
  • 需要PHP。
  • 重定向将首先执行,用户永远看不到之后的内容。header()必须是 php 脚本中的第一个命令,在输出任何其他命令之前。如果您尝试在标头之前输出一些,将收到一个Warning: Cannot modify header information - headers already sent

回答by Wintermute

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

在 JS 中设置位置的更好方法是通过:

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.

是使用 PHP 还是 JS 来管理重定向取决于您的代码在做什么以及如何做。但是,如果您能够使用 PHP;也就是说,如果您打算使用 PHP 将一些 JS 代码发送回浏览器,该代码只是告诉浏览器去其他地方,那么逻辑建议您应该去掉中间人并直接通过 PHP 告诉浏览器。

回答by Butt4cak3

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

这取决于您希望如何以及何时将用户重定向到另一个页面。

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

如果您想立即将用户重定向到另一个页面而不让他看到任何站点之间的任何内容,您应该使用 PHPheader重定向方法。

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

如果您有 Javascript 并且用户的某些操作必须导致他进入另一个页面,那么您应该使用window.location.

The metatag 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 redirecthim (e.g. to a to-be-downloaded file) without Javascript.

meta当你看到这些“下载应自动启动”的消息标签刷新上经常使用的下载站点。你可以让用户加载一个页面,等待一段时间,然后在没有 Javascript 的情况下重定向他(例如到一个要下载的文件)。

回答by krisnoble

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.

如果您可以使用 JavaScript 重定向,那么 PHP 重定向会更好,您会导致客户端在重定向之前加载页面,而使用 PHP 重定向时,它会发送正确的标头。

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.

但是 PHP 不应该放在 <head> 中,它应该在任何输出发送到客户端之前,否则会导致错误。

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.

使用 <meta> 标签与 Javascript 有相同的问题,即导致在执行重定向之前加载初始页面。如果您可以使用它们,服务器端重定向几乎总是更好。

回答by oleq

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 refererand your redirect might be tracked (referers aren't reliable in general yet this is something).

当JS关闭时,第一种情况将失败。它也有点慢,因为必须首先解析 JS(必须加载 DOM)。但是 JS 更安全,因为目的地不知道引用者并且您的重定向可能会被跟踪(引用者通常不可靠,但这是某事)。

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

您还可以使用元刷新标签。它还需要加载 DOM。