大网数据广告活动
网站运营与故障问答

Does redirecting the client using the PHP header function always work in client's modern browsers?

· 52CE 我爱测

I want to redirect users back to login page if they wrote the wrong password. I usually use header(location: "login.php?msg=wrong password"); when they type the wrong password or something. Does this method always work in modern …

问题原文

原帖链接 · John P · CC BY-SA 4.0

I want to redirect users back to login page if they wrote the wrong password. I usually use header(location: "login.php?msg=wrong password"); when they type the wrong password or something.

  1. Does this method always work in modern browsers? what about the old ones? Is there a chance that a browser won't let the redirection happen? (If it only won't work in old browsers, then how old and which browsers?)

  2. Is this the best way to redirect users? In the example the mentioned example, how do professional web developers solve this issue of redirecting users and sending the GET variables to that URL?

Note that I use header to redirect users both to the same page and sometimes to another page, my main concern is sending the client to a page (another page or the same) with the get variables set, to show a message on that page.

EDIT: I had no idea I have to even state this, but based on answers I think I should: Yes, I am aware that PHP is back end and works in the server and not client! I am asking that do modern browsers meaning the clients browser support the redirection that is send by the server. Obviously when I said "redirect client" in the title, I didn't mean redirect the server.

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

All major browsers support redirects and have done so seamlessly for 20 years.

The last browser that didn't support redirects well was Netscape 4. That was about 1997. Even then, redirects worked but:

  • The screen would flash.
  • The redirect URL was added to history. That meant that it was difficult to use the back button from the page after the redirect. Users would have to hit the back button twice really quick or they would end up just getting the redirect again.

Normally when you have questions about whether browsers support a feature you can can consult https://caniuse.com/. However, redirects have worked well for so long, that they don't feel that it is a browser feature even worth covering.

Redirecting to include parameters is fully supported as well. Your example redirect will work as you intend in all modern browsers.

Even though redirects work really well, they probably are not the correct solution to your particular problem. Ideally your login form would be produced by the same PHP file that checks the credentials. This would allow you to show the form again with the error message without doing any redirects. Typically redirects are use after successful login to take the user to whatever page they need to see next after logging in.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-11-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。