简体   繁体   English

输出消息后如何使用标题重定向到php中的另一个页面?

[英]How to redirect to another page in php using header after outputting message?

I was making an php file in which I have to add PHP Page redirection using header function after a certain condition is not true. 我正在制作一个php文件,在该文件中,必须在某些条件不成立之后使用标头函数添加PHP页面重定向

The problem is that before Redirection I have Outputted some message using echo because of this header function is not working and throwing warning:- 问题是在重定向之前,我已经使用echo输出了一些消息,因为此标头功能无法正常工作并引发警告:-

Warning: Cannot modify header information - headers already sent by (output started at C:\\xampp\\htdocs...\\search.php:229) in C:\\xampp\\htdocs...\\search.php on line 291 警告:无法修改标头信息-行291中的C:\\ xampp \\ htdocs ... \\ search.php中已经发送过的标头(输出从C:\\ xampp \\ htdocs ... \\ search.php:229开始)

Please help me with this and if there any other alternative for this please tell except that 请帮助我,如果还有其他选择,请告知

javscript method window.location=url; javscript方法window.location = url;

Sorry, in PHP there is none. 抱歉,在PHP中没有。 You simply can not modify the headers after starting to send the response body. 开始发送响应正文后,您根本无法修改标头。

There are 3 options 有3个选择

  1. Use html meta refresh 使用HTML元刷新
  2. As you mentioned use JavaScript 正如您提到的,使用JavaScript
  3. Move you code to a point where you are still able to manipulate the response header 将您的代码移动到仍然可以操纵响应头的位置

If you use modern frameworks you mostly don't have this issue. 如果您使用现代框架,则通常不会出现此问题。 Frameworks like Symphony, Laravel, Slim and others are helping you to keep your business (controller) logic separated from your display (view) logic. Symphony,Laravel,Slim等框架正在帮助您将业务(控制器)逻辑与显示(视图)逻辑分离。

For starters I would recommend the Slim framework https://www.slimframework.com . 首先,我会推荐Slim框架https://www.slimframework.com It's well documented and very lightweight. 它有据可查,非常轻巧。

You can do with this ways: 您可以通过以下方式进行操作:

With refresh and URL, this will wait five seconds to redirect you: 使用刷新和URL,这将等待五秒钟来重定向您:

header("refresh:5;url=https://www.google.com.br/");
echo "Redirecting you to Google in 5 seconds.";

Or you can use javascript: 或者您可以使用javascript:

echo '<script language="JavaScript"> alert("Redirecting you to Google BR!");window.location="https://www.google.com.br/";</script><noscript>Click here: <a href="https://www.google.com.br/">aqui</a>.</noscript>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM