views:

351

answers:

5

Can someone point me in the correct direction for researching how to prevent users from breaking out of a php script with ctrl-z, ctrl-c?

+2  A: 

Try to run it in background (add & to the end of command):

./your_php_script &
silent
What if the script requires user interaction?
GreenMatt
Of course, my solution isn't usable for scripts that requires any interaction
silent
+3  A: 

Use ignore_user_abort function. Set this in php.ini *ignore_user_abort* to true. If changed to TRUE scripts will not be terminated after a client has aborted their connection, so the script will finish.

ignore_user_abort(true);

or

$ php my_script.php -d ignore_user_abort = true
Elzo Valugi
ctrl+z will freeze it anyway, though
silent
@teksolo and @silent I never tested it, but it will be nice to know if it behaves
Elzo Valugi
This is for web interactions. However, in a comment, the OP says he wants this for a command line script.
GreenMatt
tis not just for web, you can look the documentation
silent
@silent: Okay, after re-reading the online documentation, it is not for just the web. However, *it is for client-server interactions* (which, it seems to me, are usually via the web when using PHP). It doesn't work for command line scripts (at least it didn't for me).
GreenMatt
A: 

If feasible, you can wrap the main loop in a try-catch block, and squash the Abort/Interrupt exception (not sure the exact one, sorry, my PHP is a bit rusty)

btk
While short on details, this answer's approach seems the only one for a command line script, so I don't get the downvote.
GreenMatt
+4  A: 

If you have php compiled with PCNTL (Process Control) and are not running Windows you can use pcntl_signal().

There is an example here which I modified, and it seems to catch Ctrl-C ok:

<?php
declare(ticks = 1);

pcntl_signal(SIGINT, "signal_handler");

function signal_handler($signal) {
    switch($signal) {
        case SIGINT:
            print "Ctrl C\n";
    }
}

while(1) {

}

If you try to install a handler for SIGSTP nothing happens, but I don't know why.

Tom Haigh
Additionally SIGTSTP is for Ctrl+Z
silent
SIGSTP and SIGSTOP are different signals :)
silent
@silent: thanks. i clearly don't really know what i'm talking about here
Tom Haigh
+2  A: 

There is a way to do it by having disabled in the shell.

Source is here.

#!/bin/bash
# at start add
trap "" 2 20

# your script 
echo " Test"

# loop for sleep
sleep 5

20 is for CTRL + Z

2 is for CTRL + C

and this is the full list of created by the trap -| command

1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
9) SIGKILL      10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

however to do this straight from PHP is difficult, I am not sure how to do it or if it can be done. otherwise some sites suggested javascript to capture keystrokes. hope this helps.

Justin Gregoire