How to call perl or php script from HTML file?

Easiest way to do this is call php/perl script as javascript. Since javascript is client side stuff, php or perl script need to writeback output using javascript commands only. This is useful if you have lots of HTML files and would like to do some server side processing. For example Tips & Tricks section on our site has lots of html pages which uses code as follows:

<script language="JavaScript" type="text/javascript" src="http://rss.cyberciti.biz/rss2html.php?u=http://blogs.cyberciti.biz/feed/">
<script>

Above code generates recent post posted in blogs.cyberciti.biz/hm blog. Main advantage of this technique is simple, you can use it from HTML file and your javacode is also hidden (although other better ways exists to do this).

(A) An example of PERL, create file called html.pl as follows and put into your cgi-bin directory:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $q = CGI->new();
my $info = "Server: ". $ENV{'SERVER_NAME'} . "
Script name: ". $ENV{'SCRIPT_NAME'}; # need to send header otherwise sky will fall on you print $q->header(); # always get back to user after perl code print "document.write('Hello world
I am called from HTML page $ENV{'HTTP_REFERER'}
');"; print "document.write('$info');";

Add following line to your HTML code (replace mydomain.com with your real domain name):

<script language="JavaScript" type="text/javascript" 
src="http://www.mydomain.com/cgi-bin/html.pl"
<script>

(B) An example of PHP, create file called html.php as follows and put into your webserver directory:

<?php
 $ref = $_SERVER['HTTP_REFERER'];
 $ser = "Server: ". $_SERVER['SERVER_NAME'] .  "
Script name: ". $_SERVER['SCRIPT_NAME']; echo "document.write('Hello World
I am php code called from HTML file:' );"; echo "document.write('$ref
');"; echo "document.write('$ser');"; ?>

Add following line to your HTML code (replace mydomain.com with your real domain name):

<script language="JavaScript" type="text/javascript" 
src="http://www.mydomain.com/html.php"
<script>

PHP code is bit easier to use as compare to PERL.