Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to call Perl Script from html using php

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hi folks,

I'm currently trying to find my way around with calling Perl Scripts from within an html file ...

I started with something simple:

test.pl

Code:
#!/usr/bin/perl
print "Hello \n";

Next I created an index.html file that looks something like that:

Code:
<?php
exec("/usr/bin/perl /usr/local/apache2/htdocs/test.pl")
?>

However if I try to open the index.html from within my browser it gives me a blank page. No Hello at all ...

If I extend the file like this:

Code:
<html><body><h1>It works!</h1></body></html>

<?php
exec("/usr/bin/perl /usr/local/apache2/htdocs/test.pl")
?>

It only says "It works". So at least the index.html can be opened and the html code is being executed. But what about the php/perl section ?

What's wrong here ?

By the way: I'm using Apache on AIX.

Regards,
Thomas
 
Hi

Probably your Apache is not instructed to pass files with .html extension through the PHP interpreter.

Your possibilities to make it work :
[ul]
[li]The worst solution is to put Apache to pass files with .html extension through the PHP interpreter.[/li]
[li]The bad solution is to rename the change the file's extension into .php and let the PHP interpreter execute it as PHP script.[/li]
[li]The good solution is to use SSI instead of PHP to execute the Perl script.[/li]
[li]The better solution is to use ePerl to parse the file for embedded Perl script pieces.[/li]
[li]The best solution is to use the Perl script as CGI script.[/li]
[/ul]
Note that the last three points are quite biased personal opinions. Regarding ePerl, sadly it is unmaintained since years.


Feherke.
 
The best solution sounds good ... ;-)

But what would the code have to look like for this to work ?

Regards,
Thomas
 
Hi

You mean, to use it as CGI script ? This would be a question either for forum452 or forum219.

In short, a CGI ( Common Gateway Interface ) script is one that is executed by the web server, processes data received in environment variables and on standard input and writes to the standard output whatever has to be sent back to the client.

The quick & dirty way is to write out whatever you need :
Perl:
[gray]#!/usr/bin/perl[/gray]
[b]print[/b] [green][i]"Content-type: text/html\n\n"[/i][/green][teal];[/teal]
[b]print[/b] [green][i]"<html><body><h1>It works!</h1></body></html>\n"[/i][/green][teal];[/teal]
The complex way is to use the [tt]CGI[/tt] module :
Perl:
[gray]#!/usr/bin/perl[/gray]
[b]use[/b] CGI[teal];[/teal]
[b]my[/b] [navy]$cgi[/navy][teal]=[/teal]new CGI[teal];[/teal]
[b]print[/b] [navy]$cgi[/navy][teal]->[/teal]header[teal],[/teal][navy]$cgi[/navy][teal]->[/teal]start_html[teal],[/teal][navy]$cgi[/navy][teal]->[/teal][COLOR=darkgoldenrod]h1[/color][teal]([/teal][green][i]'It works!'[/i][/green][teal]),[/teal][navy]$cgi[/navy][teal]->[/teal]end_html[teal];[/teal]
Then you put the script in your web server's cgi-bin directory. ( Or configure it to recognize CGI scripts in other way. But this is not recommended. )

But this is absolutely off-topic here.

Feherke.
 
looking at the code you posted you are missing a ';' at the end of your exec line.


I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
If it's an HTML file and your server is parsing HTML why not use SSI (Server Side Includes)..

Code:
<!--#include virtual="/cgi-bin/test.pl" -->

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top