You can do that with posix-isatty. Here is an example;
in your script.php
you can implement;
<?php
if ( !posix_isatty(STDOUT) ) {
fwrite(STDOUT, "You piped this script to another command");
exit(2);
}
fwrite(STDOUT, "Called directly");
exit(0);
?>
Update: Just for the incomers:
posix_isatty
helps you to detect if script output piped to elsewhere or not. In case above, posix_isatty(STDOUT)
means, your php script outputs to terminal. If you pipe your php script like php script | cat
, posix_isatty(STDOUT)
will be false
. Because, you have redirected output of your script to cat
as input. In other words, you redirected your script output to place that is not an terminal.