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?
Try to run it in background (add & to the end of command):
./your_php_script &
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
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)
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.
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.