8

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.

TheCatWhisperer
  • 901
  • 2
  • 12
  • 28
  • 2
    Out of curiousity, why do you want to do this? – gen_Eric Aug 14 '13 at 14:58
  • 5
    Would you be able to add a little explanation at to why you want to do this? There might be a more straight forward solution to your problem. – Joe Aug 14 '13 at 14:58
  • 4
    Triggering a client-side timeout is pretty aggressive: what's wrong with simple 400-series response – Mark Baker Aug 14 '13 at 14:58
  • I am dealing with a bot. I'm sorry that I cannot be more specific as the project is sensitive. – TheCatWhisperer Aug 14 '13 at 15:01
  • 1
    Then deny the request directly via firewall so it doesn't even reach your web server software. Web servers operate on request:reply pattern, you're asking to change the very foundation of the technology. If you want to have a timeout for a specific client, that means you can identify it - instead of passing the request to your server, simply drop it and you'll trigger a tcp wait mechanism at mentioned bot. – N.B. Aug 14 '13 at 15:10
  • 1
    Personally, I'd suggest a 450 or 451 response for naughty bots – Mark Baker Aug 14 '13 at 15:11
  • 2
    418 might be an amusing response as well – Mark Baker Aug 14 '13 at 15:15
  • It's not literally a naughty bot; but that's the closest thing to what I am dealing with. Again, I cannot be specific. – TheCatWhisperer Aug 14 '13 at 15:22
  • might http response 204 work? – Prasanth Aug 14 '13 at 16:09

4 Answers4

3

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.

Community
  • 1
  • 1
coderkane
  • 488
  • 4
  • 14
  • The sleep statement seems like it would accomplish what I want. However, I agree it is not the way to go as it might eat resources. How do you do a null response? I do not have any control over the client. – TheCatWhisperer Aug 14 '13 at 15:05
  • Well, it all depends on how the client side code will interpret your response. For example, you can do something like `echo "NULL"; exit;` but then the onus is on the client to not interpret the response not as literal string but as NULL. Same way,send 0 and exit, again the onus is the client code to interpret the 0 as empty response. So you might have to dive into the client code and see if they have any provision to treat any response as no response - usually either 0, or false, or NULL or simply no output is treated as no response. – coderkane Aug 14 '13 at 15:11
  • 1
    @Not What you really want is to have the webserver drop the connection internally without sending a drop packet to the client. In other words, just *do nothing and ignore the client*. PHP cannot really do this since the *webserver* has accepted and is holding the open connection. I don't know whether there's is an existing way to do this (also depends on the webserver), but Apache can probably be convinced to act accordingly with the right mods installed. Worst case: write your own mod. You can also do this directly at the firewall. – deceze Aug 14 '13 at 16:17
  • Although, this is not the perfect solution, it seems to do what I need it to. Thanks. – TheCatWhisperer Aug 14 '13 at 16:31
2

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.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
2

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

Andrew K.
  • 21
  • 1
-1

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

reverse_engineer
  • 4,239
  • 4
  • 18
  • 27
  • 1
    Sorry, but... so much fail in one answer! `mysql_` does not throw exceptions, so no `try..catch` will work. This will also not *delay* the response at all. Most of all though, don't make your script **crash**. All you need is to return the desired *response* to the client. This can be done explicitly without crashing your script. – deceze Aug 14 '13 at 16:12
  • @deceze Yes, you're correct about `mysql_connect` probably, but `new PDO()` should work no? The OP wants the script not to respond, this means no response and not the 'desired response', just no response... This seems like a solution to me, as well as the sleep solution... – reverse_engineer Aug 15 '13 at 11:04
  • If you want "no response" just *don't respond*. Crashing the script *will* trigger a response, so it's pointless. – deceze Aug 15 '13 at 11:17
  • @deceze Yes, seems like you're right, php just doesn't crash (in mod_php from apache anyway) I'm from a java background, so I thought it might work similarly... However the 'just don't respond' comment is what the OP is trying to achieve, and the only solution I see now is to do the sleep trick, which seems pretty bad... You know how to configure apache not to respond to certain requests? If so you should answer this question... – reverse_engineer Aug 15 '13 at 11:39
  • No, unfortunately not. I [commented above](http://stackoverflow.com/questions/18235151/do-not-respond-to-a-request-in-php/18236396?noredirect=1#comment26737650_18235297) that that's the solution to look for, but I do not know how to achieve this in practice. Apache would need so understand a signal from PHP that simply makes it drop the connection; I don't know if there's a default mod that does that or if it can be achieved writing a custom mod. – deceze Aug 15 '13 at 12:52