Calling an External CGI Script with the virtual() Function

If you are converting a site from plain HTML to PHP-enabled pages, you might have noticed that your server-side includes no longer work. If you are running PHP as an Apache module, you can use the virtual() function to call CGI scripts, such as Perl or C Web counters, and include their output in your pages. Any CGI script you write must output HTTP headers.

Let’s write a simple Perl CGI script. If you don’t know Perl, don’t worry about this. It simply outputs an HTTP header and all the environmental variables available to it:

#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
foreach ( keys %ENV ){
  print "$_: $ENV{$_}<br />\n";
}

Assuming that this script is saved in an executable ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.