开发者

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

开发者 https://www.devze.com 2022-12-29 19:59 出处:网络
Can someone point me in the correct direction for researching how to prevent users from breakin开发者_运维知识库g out of a php script with Ctrl+Z, Ctrl+C?If you have php compiled with PCNTL (Process C

Can someone point me in the correct direction for researching how to prevent users from breakin开发者_运维知识库g out of a php script with Ctrl+Z, Ctrl+C?


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.


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


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

./your_php_script &


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;
}
?>


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);


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消