Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not respond to a request in PHP

Tags:

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.

like image 578
TheCatWhisperer Avatar asked Aug 14 '13 14:08

TheCatWhisperer


3 Answers

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.

like image 200
coderkane Avatar answered Nov 11 '22 15:11

coderkane


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.

like image 45
Mr. Alien Avatar answered Nov 11 '22 14:11

Mr. Alien


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

like image 1
Andrew K. Avatar answered Nov 11 '22 15:11

Andrew K.