简体   繁体   English

在 header(), php 之前打印 echo

[英]Printing echo before header(), php

I've meet an issue with php header().我遇到了 php header() 的问题。

I have an index.php page with a contact form in it.我有一个带有联系表格的 index.php 页面。 After the form is submitted, its action goes to the same index.php page, where a handler.php script is required.提交表单后,它的操作会转到同一个 index.php 页面,其中需要一个 handler.php 脚本。 It does the following steps, if the POST has been received:如果收到 POST,它会执行以下步骤:

  1. Recaptcha check:重新验证检查:
  2. If recaptcha lets you through:如果 recaptcha 让您通过:

2.1) echo "/js script tag which alerts smth like 'captcha is verified!'/"; 2.1) echo "/js script tag which alertsmth like 'captcha is connected!'/";

2.2) require one more new_order.php script file, which sends a new order to my CRM and then redirects you through header(location ...); 2.2) 还需要一个 new_order.php 脚本文件,它会向我的 CRM 发送一个新订单,然后通过 header(location ...) 重定向您; to the abovementioned index.php page.到上述 index.php 页面。

  1. If recaptcha doesn't let you through:如果 recaptcha 不允许您通过:

3.1) echo "/js script tag which alerts smth like 'captcha is not verified!'/"; 3.1) echo "/js script tag which alertsmth like ‘captcha is not verify!'/";

If 3. happens - the page shows me a captcha-fail js-alert and automatically goes to the index.php, without calls to header();如果 3. 发生 - 页面向我显示验证码失败 js-alert 并自动转到 index.php,无需调用 header();

If 2. happens - a new CRM order is successfully created (because new_order.php does the job), header();如果 2. 发生 - 成功创建了一个新的 CRM 订单(因为 new_order.php 完成了这项工作),header(); gets me to the index.php, BUT 2.1 echo doesn't alert a message.让我进入 index.php,但 2.1 回声不会提醒消息。 It shows up, when I comment the line with 2.2) require(new_order.php).当我用 2.2) require(new_order.php) 注释该行时,它就出现了。

I need this redirect to avoid sending 'Repeat the form send'in my browser.我需要此重定向以避免在浏览器中发送“重复发送表单”。 When redirect is done, you refresh the page and it won't say 'Submit the form another time?'重定向完成后,您刷新页面,它不会说“再次提交表单?”

I know it's something about not including some info before header, but I can't grasp the main sense.我知道这是关于在标题之前不包含一些信息,但我无法掌握主要意义。

Thank you!谢谢!

Pass some additional params in each of your cases that acts like a 'flag'.在你的每个案例中传递一些额外的参数,就像一个“标志”。 Accordingly, set or dont set the custom headers() and process it conditionally.因此,设置或不设置自定义 headers() 并有条件地处理它。

不可能在标题前打印 echo。

I resolved the issue:我解决了这个问题:

I set a cookie in the end of my new_order.php script by setcookie(blah);.我通过 setcookie(blah); 在 new_order.php 脚本的末尾设置了一个 cookie。

I add an if-check in the beginning of the handler.php.我在 handler.php 的开头添加了一个 if-check。

When I redirected to the mentioned index.php, the required handler checks, whether my cookie is set.当我重定向到提到的 index.php 时,所需的处理程序会检查我的 cookie 是否已设置。

If it is - it means a have made an order, thus I receive my echo "\\script\\alert('Thank you!');\\script\\";如果是 - 这意味着已经下订单,因此我收到我的 echo "\\script\\alert('Thank you!');\\script\\";

If it is not set - I never called to the new_order.php!如果没有设置——我从来没有调用过new_order.php!

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

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