简体   繁体   English

使用PHP监控回发?

[英]Monitoring for postbacks with PHP?

I have a PHP page with content that my users can view. 我有一个PHP页面,其中包含我的用户可以查看的内容。 When this page receives a POST request from a certain external URL, I'd like to redirect the user to another page. 当此页面收到来自某个外部URL的POST请求时,我想将用户重定向到另一个页面。

The problems I'm' having are: 我遇到的问题是:

How can I monitor the page for requests being sent in an efficient way? 如何以有效的方式监控页面以发送请求?

How can I actually redirect them since header() doesn't work. 我怎么能实际重定向它们,因为header()不起作用。

Thanks. 谢谢。

For the second question, header() will work if you use it before you output any information to the page. 对于第二个问题,如果在向页面输出任何信息之前使用header(),它将起作用。 However, if you echo, print, or have HTML before it, it will give an error. 但是,如果您回显,打印或在其之前使用HTML,则会出错。

Edit : In response to Toad's comment, then you'd have to do as Aaron Harun suggested. 编辑 :回应Toad的评论,那么你必须按照Aaron Harun的建议去做。 Have the page save the $_POST data to the database or a file (make sure you sanitize it!), just like you would with any $_POST data. 让页面将$ _POST数据保存到数据库或文件中(确保清理它!),就像使用任何$ _POST数据一样。 You would then need to use AJAX to get a response from a second PHP page that simply checks for the existence of updated data from wherever you saved it. 然后,您需要使用AJAX从第二个PHP页面获取响应,该页面只是检查是否存在来自保存它的更新数据。 If the response comes back true, then you redirect using a JavaScript redirect. 如果响应返回true,则使用JavaScript重定向重定向。

The only other way to do it without using AJAX would be to refresh the page using an HTML meta refresh element at timed intervals to check if $_POST data has been received. 不使用AJAX的另一种方法是使用HTML元刷新元素以定时间隔刷新页面,以检查是否已收到$ _POST数据。 HTML would perform the refresh, PHP would do the checking, and you could use either for the redirect. HTML将执行刷新,PHP将执行检查,您可以使用其中一个进行重定向。

You can use an AJAX script to "ping" the server intermittently to see if there have been any changes. 您可以使用AJAX脚本间歇性地“ping”服务器以查看是否有任何更改。 If there are, redirect the user with JavaScript. 如果有,请使用JavaScript重定向用户。

It's pretty much the only way. 这几乎是唯一的方式。

we can put this snippet to check the post request 我们可以把这个片段用来检查帖子请求

if($_POST['flag']==1) {
          header("location:newpage.php");
          exit();
}

But If you want to check the request regularly without user interaction than you will have to use AJAX 但是,如果您想在没有用户交互的情况下定期检查请求,则必须使用AJAX

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

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