PHP 中的重定向

发布于 2024-11-27 06:02:25 字数 505 浏览 2 评论 0原文

如果我想在 PHP 中重定向用户,我所知道的就是使用 header('Location:' http ://www.example.com),但我一直在读到这不是在内部将用户从一个页面重定向到另一个页面的最佳方式。您可以重定向用户的其他选项有哪些?

示例:底部写着:

需要记住的重要事情
...例如,我不建议使用 header() 将用户跳转到不同的页面;有更好的方法可以减少页面加载次数并为用户提供更流畅的体验......

http://tinsology.net/2009 /06/创建安全登录系统的正确方式/

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

过期以后 2024-12-04 06:02:25

您提供的代码片段指的是 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 by header('Lodation: http://www.example.com/page2.php'); and where page2.php then executes some code followed by header('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.

够运 2024-12-04 06:02:25

这还不错。然而,您可以添加 301 响应代码以使其更好,这也更好地让 Google 确定他不应该再访问该“旧”网站。

<?php
Header( "HTTP/1.1 301 Moved Permanently" ); 
Header( "Location: http://www.new-url.com" ); 
?> 

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
Header( "HTTP/1.1 301 Moved Permanently" ); 
Header( "Location: http://www.new-url.com" ); 
?> 
夏有森光若流苏 2024-12-04 06:02:25

这是基于 PHP 的重定向的公认方法。如果您可以在 PHP 脚本执行之前完成重定向,那么您应该通过 .htaccess 或服务器级别名。

查看 header 的手册:http://php.net /manual/en/function.header.php

This 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

糖果控 2024-12-04 06:02:25

避免在内部使用 header() 总是明智的,因为有时标头已经被调用。 JavaScript 重定向非常好,并且在许多 Web 应用程序中系统地使用。浏览器和搜索引擎不会歧视或不喜欢 JavaScript 重定向。一个简单的例子:

<?php echo '<script type="text/javascript">window.location.href="index.php"</script>'; ?>

或者使用变量:

<?php echo '<script type="text/javascript">window.location.href="' . $page . '"</script>'; ?>

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:

<?php echo '<script type="text/javascript">window.location.href="index.php"</script>'; ?>

Or using a variable:

<?php echo '<script type="text/javascript">window.location.href="' . $page . '"</script>'; ?>
哭了丶谁疼 2024-12-04 06:02:25

只需使用

header("Location: http://www.example.com");

Just use

header("Location: http://www.example.com");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文