PHP 中的重定向
如果我想在 PHP 中重定向用户,我所知道的就是使用 header('Location:' http ://www.example.com),但我一直在读到这不是在内部将用户从一个页面重定向到另一个页面的最佳方式。您可以重定向用户的其他选项有哪些?
示例:底部写着:
需要记住的重要事情
...例如,我不建议使用 header() 将用户跳转到不同的页面;有更好的方法可以减少页面加载次数并为用户提供更流畅的体验......
If I want to redirect a user in PHP, all I've ever known to do was use the header('Location:' http://www.example.com) but I've been reading that this isn't the best way to redirect a user from page to page internally. What are some other options you can redirect a user?
Example: at the bottom it says:
Something Important to Remember
...I don’t recommend, for example, using header() to bounce your users around to different pages; there are better methods that reduce the number of page loads and give the user a more fluid experience...
http://tinsology.net/2009/06/creating-a-secure-login-system-the-right-way/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您提供的代码片段指的是
page1.php
可能执行一些代码,后跟header('Lodation: http://www.example.com/page2.php');
然后page2.php
执行一些代码,然后执行header('位置:http://www.example.com/page3.php');
等等。对于用户体验来说非常很糟糕,而且对于管理代码也不是很好。如果您确实需要重定向用户(301 重定向可能是最常见的),那么使用
header
是完全可以接受的。The snippet you provided is referring to issues where
page1.php
might execute some code followed byheader('Lodation: http://www.example.com/page2.php');
and wherepage2.php
then executes some code followed byheader('Location: http://www.example.com/page3.php');
etc. This is very bad for user experience, and not very good for managing code either.In cases where you genuinely need to redirect a user (301 redirect is probably the most common), using
header
is perfectly acceptable.这还不错。然而,您可以添加 301 响应代码以使其更好,这也更好地让 Google 确定他不应该再访问该“旧”网站。
It isn't bad. However you could add 301 response code to make it more better, it is also better for Google to determine he should not visit that "old" site anymore.
这是基于 PHP 的重定向的公认方法。如果您可以在 PHP 脚本执行之前完成重定向,那么您应该通过 .htaccess 或服务器级别名。
查看
header
的手册:http://php.net /manual/en/function.header.phpThis is the accepted method for PHP-based redirection. If you can accomplish the redirect prior to PHP script execution, then you should - through .htaccess or server-level aliasing.
Check out the manual on
header
: http://php.net/manual/en/function.header.php避免在内部使用
header()
总是明智的,因为有时标头已经被调用。 JavaScript 重定向非常好,并且在许多 Web 应用程序中系统地使用。浏览器和搜索引擎不会歧视或不喜欢 JavaScript 重定向。一个简单的例子:或者使用变量:
It's always sensible to avoid using
header()
internally because sometimes headers are already called. JavaScript redirects are absolutely fine and are used systematically in a lot of web applications. Browsers and search engines don't discriminate against or dislike JavaScript redirects. A simple example:Or using a variable:
只需使用
Just use