Question

I am unable to understand and run a simple PHP script in FCGI mode. I am learning both Perl and PHP and I got the Perl version of FastCGI example below to work as expected.

Perl FastCGI counter:

#!/usr/bin/perl

use FCGI;

$count = 0;

while (FCGI::accept() >= 0) {

    print("Content-type: text/html\r\n\r\n",
          "<title>FastCGI Hello! (Perl)</title>\n",
          "<h1>FastCGI Hello! (Perl)</h1>\n",
          "Request number ", $++count,
          " running on host <i>$ENV('SERVER_NAME')</i>");

}

Searching for similar in PHP found talk about "fastcgi_finish_request" but have no clue how to accomplish the counter example in PHP, here is what I tried:

<?php
header("content-type: text/html");
$counter++;
echo "Counter: $counter ";
//http://www.php.net/manual/en/intro.fpm.php
fastcgi_finish_request(); //If you remove this line, then you will see that the browser has to wait 5 seconds
sleep(5);
?>
Was it helpful?

Solution

Perl is not PHP. This must not mean that you can not most often interchange things and port code between the two, however when it comes to runtime environments there are bigger differences you can not just interchange.

FCGI is on the request / protocol level already which is fully abstracted in the PHP runtime and you therefore have not as much control in PHP as you would have with Perl and use FCGI;

Therefore you can not just port that code.

Next to that fastcgi_finish_request is totally unrelated to the Perl code. You must have confused it or thrown it in by sheer luck to give it a try. However it's not really useful in this counter example context.

OTHER TIPS

PHP and HTTP are stateless. All data is only relevant for the current, ongoing request. If you need to save state, you might consider storing the data into cookie, session, cache or db.

So the implementation of this "counter" example will be different for PERL and PHP.

Your usage of fastcgi_finish_request won't bring the functionality you expect from PERL. Think about a long running calculation, where you output data in the middle. You can do that with fastcgi_finish_request, the data is then pushed to the browsers, while the long running tasks keeps running.

Opening happens together FASTCGI+PHP. Normally the connection would be open till PHP finishes, then FASTCGI would be closed. Except you reach the exec timeout of PHP (exec timeout) or fastcgi timeout (connection timeout). fastcgi_finish_request handles the case, where the fascgi connection to the browser is closed BEFORE PHP finishes execution.

Simple Hit Counter Example for PHP

<?php
$hit_count = @file_get_contents('count.txt'); // read count from file
$hit_count++; // increment hit count by 1

echo $hit_count; //  display

@file_put_contents('count.txt', $hit_count); // store the new hit count
?>

Honestly, that's not even how you should do it using Perl either.

Instead, I'd recommend using CGI::Session to track session information:

#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Session;

my $q = CGI->new;
my $session = CGI::Session->new($q) or die CGI->Session->errstr;
print $session->header();

# Page View Count
my $count = 1 + ($session->param('count') // 0);
$session->param('count' => $count);

# HTML
print qq{<html>
<head><title>Hello! (Perl)</title></head>
<body>
<h1>Hello! (Perl)</h1>
<p>Request number $count running on host <i>$ENV{SERVER_NAME}</i></p>
</body>
</html>};

Alternatively, if you really want to go barebones, you could keep a local file as demonstrated in: I still don't get locking. I just want to increment the number in the file. How can I do this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top