How to disable ctrl-z, ctrl-c from breaking out of a php script

不想你离开。 提交于 2019-12-03 15:28:28

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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

./your_php_script &



回答5:


If you use the numeric values (rather than the built-in PHP constants) then it seems to work, at least for me, on a Debian/Ubuntu system.

Oddly enough, in my example below, the callback function signal_handler() is not being called. Instead, it just displays "^Z" and "^C" on the input line, while the signals are simply being ignored. In the end, no break/suspend is possible, which hopefully answers the original question:

#!/usr/bin/php
<?
pcntl_signal( 2, "signal_handler");
pcntl_signal( 20, "signal_handler");

function signal_handler( $signal )
{
  echo( "This is not even invoked..?\n" );
}

$end = FALSE;

while( $end == FALSE )
{
  echo( "Enter Q to quit: " );
  $fp = fopen( "php://stdin", "r" );
  $line = fgets( $fp ); 
  $line = str_replace( "\n", "", $line );
  fclose( $fp );

  if( strtolower($line) == "q" ) $end = TRUE;
}
?>



回答6:


If you just need your script to ignore a specific signal, use the SIG_IGN constant option. For SIGINT signal the code would be

pcntl_signal(SIGINT, SIG_IGN);



回答7:


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)



来源:https://stackoverflow.com/questions/2774331/how-to-disable-ctrl-z-ctrl-c-from-breaking-out-of-a-php-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!