简体   繁体   English

不要回复PHP中的请求

[英]Do not respond to a request in PHP

I know that I can abort a script in PHP by calling exit or die. 我知道我可以通过调用exit或die来中止PHP中的脚本。 However, I would like not only to stop execution of a script, but also not even send a response to certain requests (resulting in a client side timeout). 但是,我不仅希望停止执行脚本,而且甚至不发送对某些请求的响应(导致客户端超时)。

If anyone could tell me how to do this I would really appreciate it. 如果有人能告诉我怎么做,我真的很感激。 Thanks! 谢谢!

EDIT: I am sorry that I cannot be more specific as to why I am doing this, but I cannot because of the nature of the project. 编辑:对不起,我不能更具体地说明为什么我这样做,但我不能因为项目的性质。 What I will say is that I am dealing with a bot. 我要说的是我正在处理一个机器人。

Client side timeout? 客户端超时? Probably you want to end the user's session so you can simply use session_destroy() before you die or exit your script. 可能你想结束用户的会话,所以你可以在dieexit脚本之前简单地使用session_destroy()

Also it is weird that instead of redirecting the user with a notice, you are ending the script execution which is not friendly. 同样奇怪的是,不是通过通知重定向用户,而是结束了不友好的脚本执行。 If you want, you can use header() to redirect the user to some page which may throw him a notice about what he did or why he was prevented/redirected. 如果你愿意,你可以使用header()将用户重定向到某个页面,这可能会让他知道他做了什么或者为什么要阻止/重定向。

Client side timeout means when you make an ajax request but the server takes too long to respond. 客户端超时意味着您发出ajax请求但服务器响应时间过长。 If you really really want to accomplish this, I guess the way would be to put a big sleep() statement, so that the ajax page does not respond and it times out. 如果你真的想要完成这个,我想方法就是放一个大的sleep()语句,这样ajax页面就不会响应而且会超时。 However, I cannot stress enough how this is not the way to do it, in fact its inviting a future disaster. 但是,我不能强调这是不是这样做的方式,实际上是它邀请未来的灾难。

Ideally you should send a specific response back to the client, eg blank, or "0" or "NULL", and then in the client side code treat that response as a no response. 理想情况下,您应该将特定响应发送回客户端,例如空白,或“0”或“NULL”,然后在客户端代码中将该响应视为无响应。

If you have no control over client side code eg, a bot, I would second Mark Baker's comment and say send a 444 or if you are feeling funny, 418 response :) 如果您无法控制客户端代码,例如机器人,我会选择Mark Ba​​ker的评论并说发送444或者如果您感觉好笑,418响应:)

This post explains how to send a HTTP response code using PHP. 这篇文章解释了如何使用PHP发送HTTP响应代码。

Have the same problem. 有同样的问题。 Need to not respond, nothing, just stop. 需要不回应,没有,只是停下来。 Found nothing, and use: 什么都没找到,并使用:

posix_kill(posix_getpid(), 9);

It just kills php process. 它只是杀死php进程。 Not a god solution but works for me 不是上帝的解决方案,但对我有用

The problem is that when the PHP executes, it always generates a response... even echo '' will be sent back. 问题是,当PHP执行时,它总是生成一个响应...甚至echo ''将被发回。

The best way to do this is to make the script crash, I think... 执行此操作的最佳方法是使脚本崩溃,我认为......

Just do something like: $pdo = new PDO('unexisting_server'); 只需执行以下操作: $pdo = new PDO('unexisting_server'); without a try catch block 没有尝试catch块

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

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