简体   繁体   中英

Do not respond to a request in PHP

I know that I can abort a script in PHP by calling exit or die. 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.

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.

Client side timeout means when you make an ajax request but the server takes too long to respond. 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. 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.

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 :)

This post explains how to send a HTTP response code using PHP.

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. 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.

The best way to do this is to make the script crash, I think...

Just do something like: $pdo = new PDO('unexisting_server'); without a try catch block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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