Check if php cgi

How can I check if stdin exists in PHP ( php-cgi )?

Here is my code: Then from Postman I send POST request at my server url like POST http://myserverurl/index.php I do send post data on above request, I have tried form-encoded, binary and raw too. Question: I am unable to get fopen to read content stream in PHP.

How can I check if stdin exists in PHP ( php-cgi )?

Setup and Background

I am working on script that needs to run as /usr/bin/php-cgi instead /usr/local/bin/php and I’m having trouble checking for stdin

If I use /usr/local/bin/php as the interpreter I can do something like

This doesn’t seem to work with php-cgi — Looks to always be undefined. I checked the man page for php-cgi but didn’t find it very helpful. Also, if I understand it correctly, the STDIN constant is a file handle for php://stdin . I read somewhere that constant is not supposed to be available in php-cgi

Requirements
  • The shebang needs to be #!/usr/bin/php-cgi -q
  • The script will sometimes be passed arguments
  • The script will sometimes receive input via STDIN
Current Script
#!/usr/bin/php-cgi -q fclose($fh); > echo $stdin; 
Problematic Behavior
$ echo hello | ./myscript.php hello 
These things don’t work for me:
  • Checking defined(‘STDIN’) // always returns false
  • Looking to see if CONTENT_LENGTH is defined
  • Checking variables and constants

I have added this to the script and run it both ways:

print_r(get_defined_constants()); print_r($GLOBALS); print_r($_COOKIE); print_r($_ENV); print_r($_FILES); print_r($_GET); print_r($_POST); print_r($_REQUEST); print_r($_SERVER); echo shell_exec('printenv'); 

I then diff’ed the output and it is the same.

I don’t know any other way to check for / get stdin via php-cgi without locking up the script if it does not exist.

/usr/bin/php-cgi -v yields: PHP 5.4.17 (cgi-fcgi)

You can use the select function such as:

$stdin = ''; $fh = fopen('php://stdin', 'r'); $read = array($fh); $write = NULL; $except = NULL; if ( stream_select( $read, $write, $except, 0 ) === 1 ) < while ($line = fgets( $fh )) < $stdin .= $line; >> fclose($fh); 

Regarding your specific problem of hanging when there is no input: php stream reads are blocking operations by default. You can change that behavior with stream_set_blocking() . Like so:

$fh = fopen('php://stdin', 'r'); stream_set_blocking($fh, false); $stdin = fgets($fh); echo "stdin: '$stdin'"; // immediately returns "stdin: ''" 

Note that this solution does not work with that magic file handle STDIN .

stream_get_meta_data helped me 🙂

And as mentioned in the previous answer by Seth Battin stream_set_blocking($fh, false); works very well 👍

Читайте также:  Javascript выбрать первую строку

The next code reads data from the command line if provided and skips when it’s not.

For example: echo «x» | php render.php and php render.php

In the first case, I provide some data from another stream (I really need to see the changed files from git, something like git status | php render.php .

Here is an example of my solution which works:

$input = []; $fp = fopen('php://stdin', 'r+'); $info = stream_get_meta_data($fp); if (!$info['seekable'] && $fp) < while (false !== ($line = fgets($fp))) < $input[] = trim($line); >fclose($fp); > 

The problem is that you create a endless loop with the while($line = fgets($fh)) part in your code.

$stdin = ''; $fh = fopen('php://stdin','r'); if($fh) < // read *one* line from stdin upto "\r\n" $stdin = fgets($fh); fclose($fh); >echo $stdin; 

The above would work if you’re passing arguments like echo foo=bar | ./myscript.php and will read a single line when you call it like ./myscript.php If you like to read more lines and keep your original code you can send a quit signal CTRL + D

To get parameters passed like ./myscript.php foo=bar you could check the contents of the $argv variable, in which the first argument always is the name of the executing script:

 ./myscript.php foo=bar // File: myscript.php $stdin = ''; for($i = 1; $i

I’m not sure that this solves anything but perhaps it give you some ideas.

Pseudo-terminal will not be allocated because stdin is not a terminal, Missing: cgi ( | Must include:

Non-blocking on STDIN in PHP CLI

Is there anyway to read from STDIN with PHP that is non blocking:

stream_set_blocking(STDIN, false); echo fread(STDIN, 1); 
$stdin = fopen('php://stdin', 'r'); stream_set_blocking($stdin, false); echo 'Press enter to force run command. ' . PHP_EOL; echo fread($stdin, 1); 

but it still blocks until fread gets some data.

I noticed a few open bug reports about this (7 years old), so if it can’t be done, does any one know any crude hacks that could accomplish this (on Windows and Linux)?

  • https://bugs.php.net/bug.php?id=34972
  • https://bugs.php.net/bug.php?id=47893
  • https://bugs.php.net/bug.php?id=48684

Here’s what I could come up with. It works fine in Linux, but on Windows, as soon as I hit a key, the input is buffered until enter is pressed. I don’t know a way to disable buffering on a stream.

Just a notice, that non blocking STDIN working, now.

system('stty cbreak'); while(true) < if($char = fread(STDIN, 1)) < echo chr(8) . mb_strtoupper($char); >> 

Petah, I can’t help with the PHP side of this directly, but I can refer you to an article I ran across a while ago in which someone emulated transistors by testing within a shell script for the existence of pending data for a named pipe. It’s a fascinating read, and takes shell scripting to a whole new level of geekiness. 🙂

The article is here: http://www.linusakesson.net/programming/pipelogic/

So . in answer to your «crude hacks» request, I suppose you could shunt your stdio through named pipes, then exec() the tool whose source is included at the URL above to test whether anything is waiting to be sent through the pipe. You’d probably want to develop some wrapper functions to help with stuff.

Читайте также:  Java system exit method

I suspect the pipelogic solution is Linux-only, or at least would require a unix-like operating system. No idea how this could be accomplished on Windows.

Detecting errors during PHP’s parsing of multidimensional POST data, The only way to detect this would be to write your own x-www-form-urlencoded parser that checks for it, and have it read php://stdin .

Fopen returns empty content for php://stdin

I am unable to get fopen to read content stream in PHP. There is no error thrown, the content is always empty string.

$contents=''; $handle = fopen("php://stdin", "r") or error_log('got some error'); while (!feof($handle)) < $contents .= fread($handle, 8192); >fclose($handle); error_log($contents); 

Then from Postman I send POST request at my server url like

I do send post data on above request, I have tried form-encoded, binary and raw too.

But error_log logs empty string to the log file, which means that fopen did actually work but it got empty content.

I have checked php.ini settings as well and allow_url_fopen setting is true as well.

I am testing this on nginx with PHP7.1-fpm. I recently switched from Apache and PHP 5.6 to nginx and php7.1-fpm and it stopped working.

It was totally working fine with Apache and PHP5.6.

What is wrong here? Any help will be highly appreciated.

There is probably some misunderstanding in the default streams of PHP.

  • If you’re running the PHP script as a command, the CLI interface would give you php://stdin.
  • If you’re running the PHP script in a web server / cgi / php-fpm environment, there is no php://stdin at all. You’ll be getting php://input stream (for the POST body) instead.

Reference: Supported Protocol and Wrapper

PHP — how to best determine if the current invocation is from CLI or, I need to determine whether the current invocation of PHP is from the command line (CLI) or from the web server (in my case,

Источник

Check if php-cgi server is connectable remotely

I have a php server listening on a port that seems to have issues. How can I connect to the php-cgi server to find out it if it up or not? (Sort of «ping» the php-cgi process)

I need to determine if the issue I’m having is due to the http server or the php-cgi server. Therefor I want to be able to «ping» the php-cgi server to make sure it is accepting requests. If not, I know it is the php-cgi server that is the cause of the problem.

Answer

Solution:

telnet localhost portnumber.

So if you running on port 80 of your localhost type in

It should connect if it does not, your server is not running!

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Читайте также:  Android java убрать клавиатуру

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

PHP execution modes – FPM versus FCGId versus CGI wrapper

This article describes the PHP execution modes present in a Virtualmin installation.

We’ve extracted some of the information from the help bubble provided by Virtualmin at the following menu:
Virtual Server Name => Server Configuration => PHP Options

The three modes present are:

FPM is the best because it fast and also conserves memory better than FCGId. Both run PHP scripts quickly and as the domain owner.

Unless your system is heavily loaded or security between users is not an issue, CGI Wrapper could be an option.

mod_php is not recommended at all, and shouldn’t be on your system. It’s small gain in performance should be balanced against the lack of security between virtual servers.

As of Virtualmin version 7.x the control panel will offer an option to remove mod_php directives as it’s use has been discouraged for years.

Troubleshooting

How to check if you have lingering php-cgi processes

Sometimes after migration or incorrectly set up systems, you might encounter that some sites are still running php-cgi instead of FPM. To find out which sites are running php-cgi, use this command:

> ps -ef | grep php-cgi example1+ 3158626 3123276 0 00:33 ? 00:00:07 /bin/php-cgi7.4 example1+ 3158652 3123276 0 00:33 ? 00:00:07 /bin/php-cgi7.4 example2 3177409 3123276 0 00:41 ? 00:00:01 /bin/php-cgi7.4 example2 3183811 3123276 0 00:44 ? 00:00:02 /bin/php-cgi7.4

If you see any php-cgi processes, we recommend you change them manually to FPM, or get the professional version of Virtualmin where you can do this in bulk:

Источник

Оцените статью