...

View Full Version : Include and Perl script help



sonny
08-02-2009, 07:48 PM
Hi

I use this on the few remaining html pages I use
<!--#include virtual="/do-this.php"-->

I have been trying to include (run this) from within
a perl script, I must have tried a million ways so far

One quick example being

$do_file = "do-this.php";
print "Content-type: text/html\n\n"; open(FILE,"$do_file"); while() { print $_; } exit;

the only way I can get this to work so far, is via JS, or a image
tag, which does not work in all cases and of course does
not work for search engines.

I'm looking for a solution that will do the same thing as this
<!--#include virtual="/do-this.php"--> does on html pages if possible,
if there is no way to do this can someone confirm this and I will give
up on this.

This is one of my favorite scripts it has never given me a problem in
over 8 years, I would hate to have to use a php version of it.

Thanks
Sonny

FishMonger
08-03-2009, 12:07 AM
Is this what you're looking for?

my $php_output = `/do-this.php`;

print "Content-type: text/html\n\n";

print $php_output;

sonny
08-03-2009, 02:01 AM
Is this what you're looking for?

my $php_output = `/do-this.php`;

print "Content-type: text/html\n\n";

print $php_output;


NO

Make that one million and 1



#!/usr/bin/perl

my $php_output = `/test.php`;

print "Content-type: text/html\n\n";

print <<ENDOFHTML;
<html><head><title>hello world</title></head>
<body> <table width="100%">
<tr><td align='center'>Hello World!</td></tr></table><br />
ENDOFHTML

print "<font color=green'>bye!</font></body></html>\n";
print $php_output;



That does not work as well
Sonny

FishMonger
08-03-2009, 02:25 AM
Have you tried swaping these 2 lines?

print "<font color=green'>bye!</font></body></html>\n";
print $php_output;

If that doesn't do what you want, then please give more details of what you need.

sonny
08-03-2009, 02:41 AM
Have you tried swaping these 2 lines?

print "<font color=green'>bye!</font></body></html>\n";
print $php_output;

If that doesn't do what you want, then please give more details of what you need.

OK swapped them and got the same result,
common You had a feeling it would not work right?

I have been playing with this for over
2 months now, I knew it was not that simple.

I am trying to run and display the follwing
PHP page within a perl script footer, this
works fine in html and PHP pages



<?
require "/home/content/html/connect.php";
include "/home/content/html/log-insert.php";
?>
<?
$query=" SELECT * FROM visits ORDER BY id DESC LIMIT 5";
$result=mysql_query($query) or die(mysql_error());
while($row =mysql_fetch_array($result))
{

$flag=$row['flag'];
$country=$row['country'];

echo " <b>$country)</b> &nbsp;<img src='/images/flags/$flag.png' width=18 height=12>";
echo "<br>";
}
?>

bazz
08-03-2009, 03:57 AM
what error message are you getting, either in your script or server errors log?

At a guess (at 3am), you may need to start with putting your <? .... php tags inside a print statement. Likely you'll need to try other things as well.

bazz

sonny
08-03-2009, 04:14 AM
what error message are you getting, either in your script or server errors log?

At a guess (at 3am), you may need to start with putting your <? .... php tags inside a print statement. Likely you'll need to try other things as well.

bazz

No what?

You may be on to something
Its a mere 10.12pm here, and I am calling it a night
I will try that tomorrow and post back

Thank you for weighing in on this
Sonny

FishMonger
08-03-2009, 05:03 AM
Telling us that it doesn't work or that you get the same result without telling us what that result was is meaningless to us.

What output did you get?
How did that output differ from what you expected?
What, if any, was the error in the web server error log?
Do you have warnings enabled?
Are you directing the errors and warnings to the browser?

If you're not doing anything "special" in the php script that Perl can't handle, which is extremely unlikely, then the best solution is to drop the php script and do everything it does directly in the Perl script.

You should do that db call in Perl rather than php.
DBI - http://search.cpan.org/~timb/DBI-1.609/DBI.pm

KevinADC
08-03-2009, 06:06 AM
I would be a little surprised if this works:

my $php_output = `/test.php`;

that seems like it would need to fetch a webpage, which I am not sure the above would do.

sonny
08-03-2009, 05:32 PM
Telling us that it doesn't work or that you get the same result without telling us what that result was is meaningless to us.

What output did you get?
How did that output differ from what you expected?
What, if any, was the error in the web server error log?
Do you have warnings enabled?
Are you directing the errors and warnings to the browser?

If you're not doing anything "special" in the php script that Perl can't handle, which is extremely unlikely, then the best solution is to drop the php script and do everything it does directly in the Perl script.

You should do that db call in Perl rather than php.
DBI - http://search.cpan.org/~timb/DBI-1.609/DBI.pm

I thought I was very clear in my first post, if not sorry

I can tell you for sure, I am not trying to run the world with that php
include for testing purposes, I am simple trying to include a php page

So for testing sake, lets keep things very simple
Below is the test Perl script



#!/usr/bin/perl

my $php_output = `test.php`;

print "Content-type: text/html\n\n";

print <<ENDOFHTML;
<html><head><title>hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
ENDOFHTML

print "$php_output";
print "<center><h3><b>my php include should display directly Above in Red text</h3></center></b></body></html>\n";


Below is the test Php page I would like to include in that Perl script above


<html>
<head>
<body>
<?PHP
print '<h2><font color=ff0000><center>I am a php include</h2></center></font>';
?>
</body>
</html>


My expectation is to see the php include text when I call that perl script

Sonny

FishMonger
08-03-2009, 05:44 PM
Execute the php script by executing the php interpreter and pass the script as its parameter and don't print the headers in the php script because that will give you 2 sets of headers, which you don't want.

Try this:
test.php

<?PHP
print '<h2><center>I am working relax!<br>Yours truely PHP include</h2></center>';
?>

test.pl

#!/usr/bin/perl

my $php_output = `php test.php`;

print "Content-type: text/html\n\n";

print <<ENDOFHTML;
<html><head><title>hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
ENDOFHTML

print "$php_output";
print "<center><h3><b><font color=red>my php include should display directly below in black text</h3></center></font></b></body></html>\n";

FishMonger
08-03-2009, 05:48 PM
The php script could simple be just 1 line, the print statement.



[root@fc4dev ~]# cat test.php
<h2><center>I am working relax!<br>Yours truely PHP include</h2></center>;

[root@fc4dev ~]# cat test.pl
#!/usr/bin/perl

my $php_output = `php test.php`;

print "Content-type: text/html\n\n";

print <<ENDOFHTML;
<html><head><title>hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
ENDOFHTML

print "$php_output";
print "<center><h3><b><font color=red>my php include should display directly below in black text</h3></center></font></b></body></html>\n";

[root@fc4dev ~]# ./test.pl
Content-type: text/html

<html><head><title>hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
<h2><center>I am working relax!<br>Yours truely PHP include</h2></center>;

<center><h3><b><font color=red>my php include should display directly below in black text</h3></center></font></b></body></html>

sonny
08-03-2009, 06:17 PM
No that did not work either, I used the following to test
Sonny


#!/usr/bin/perl

#test.cgi

my $php_output = `test.php`;
print "Content-type: text/html\n\n";

print <<ENDOFHTML;
<html><head><title>Hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
ENDOFHTML

print "$php_output";
print "<center><h3><b>PHP include should display directly above in red</h3></center></b></body></html>\n";





<?PHP

//test.php

print '<h2><b><fon color=FF0000><center>I am working relax!<br>Yours truely PHP include</h2></b></font></center>';
?>

FishMonger
08-03-2009, 06:24 PM
Did you try what I suggested?


my $php_output = `php test.php`;

sonny
08-03-2009, 06:32 PM
Did you try what I suggested?


my $php_output = `php test.php`;



Yes that did not work as well


$php_output = `*php* test.php`;


Sonny

FishMonger
08-03-2009, 06:36 PM
Why would you think that would work?

Does that look like what I showed you? NO it doesn't.

sonny
08-03-2009, 07:21 PM
Why would you think that would work?

Does that look like what I showed you? NO it doesn't.

none work, at this point I am desperate
$php_output = `php test.php`;
my $php_output = `php test.php`;

don't take my word for it, just place the two files mentioned
in your root, and call test.cgi.


To add additional info
this is what the page looks like, when I use
my $php_output = `php test.php`;



Hello this is a Test

Content-type: text/html $php_output = `php test.php`; print "Content-type: text/html\n\n"; print <
Hello this is a Test

ENDOFHTML print "$php_output"; print "
PHP include should display directly above in red
\n";
PHP include should display directly above in red


Think about this for a moment? we are trying to include one of the most basic php pages for testing
you could ask for and cannot do it.

FishMonger
08-03-2009, 07:45 PM
First test it from the command line. Once you've verified that to work, then we can move on to running as a cgi script. If it fails as a cgi script that would indicate the issue to be the difference in the environments.

Here's my working test from the command line.



[root@fc4dev ~]# cat test.php
<?PHP
print '<h2><center>I am working relax!<br>Yours truely PHP include</h2></center>';
?>



[root@fc4dev ~]# cat test.pl
#!/usr/bin/perl

my $php_output = `php test.php`;

print "Content-type: text/html\n\n";

print <<ENDOFHTML;
<html><head><title>hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
ENDOFHTML

print "$php_output";
print "<center><h3><b><font color=red>my php include should display directly below in black text</h3></center></font></b></body></html>\n";



[root@fc4dev ~]# ./test.pl
Content-type: text/html

<html><head><title>hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
<h2><center>I am working relax!<br>Yours truely PHP include</h2></center>
<center><h3><b><font color=red>my php include should display directly below in black text</h3></center></font></b></body></html>


Now, test that and verify that it works.

If it doesn't work, then you have an environment issue and may need to use the full path when specifying the php interpreter and the script. On my system, the interpreter in at /usr/bin/php

When you run it in a cgi environment, you will want to specify the full paths and the php script may need to be outside the ScriptAlais directory. So far, I have not tested it in the cgi environment.

FishMonger
08-03-2009, 07:52 PM
I just tested it as shown as a cgi script and it worked as expected. If it doesn't work for you, then you have a problem with your system setup, not the test script.

KevinADC
08-03-2009, 07:59 PM
I tried like this as a CGI:



#!/usr/bin/perl

#test.cgi

my $php_output = `/usr/local/bin/php $ENV{DOCUMENT_ROOT}/test.php`;
print "Content-type: text/html\n\n";

print <<ENDOFHTML;
<html><head><title>Hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
ENDOFHTML

print "<span style=\"color: red\">$php_output</span></body></html>";


The perl script is in the cgi-bin and the php document is in the web root. the PHP document is set at 755 and works when accessed directly from its url, but not when accessed from the perl script. No errors in the server error log.

KevinADC
08-03-2009, 08:04 PM
I just tested it as shown as a cgi script and it worked as expected. If it doesn't work for you, then you have a problem with your system setup, not the test script.

Are you testing on a local server or a web hosting server with a shared account? I tested on a shared hosted account and its not working for me.

FishMonger
08-03-2009, 08:14 PM
Works for me when I moved the php script to the document root. I tested with permission settings of php script with 755 and 644.

Both worked for me calling them as:

calling php script alone
http://192.168.0.60/test.php

calling pl script
http://192.168.0.60/cgi-bin/test.pl

This is the line I used in the Perl script.

my $php_output = `php $ENV{DOCUMENT_ROOT}/test.php`;

FishMonger
08-03-2009, 08:18 PM
I tested it on my local server.

If it doesn't work on a public server, then you need to check the environment and possibly use one of Perl's other methods of executing the php script and retrieving the output.

KevinADC
08-03-2009, 08:29 PM
Doesn't work for me on my shared hosted account no matter what I try.

KevinADC
08-03-2009, 08:31 PM
I tested it on my local server.

If it doesn't work on a public server, then you need to check the environment and possibly use one of Perl's other methods of executing the php script and retrieving the output.

I'll let the OP figure it out, I've tried and tested as much as I can. Getting near lunch time too :thumbsup:

FishMonger
08-03-2009, 08:34 PM
Getting near lunch time too :thumbsup:

I have to skip lunch and instead go to a doctor's appointment. :(

KevinADC
08-03-2009, 08:44 PM
I have to skip lunch and instead go to a doctor's appointment. :(

Hopefully not for a prostrate examine, although I have to admit, they don't really bother me anymore. Not sure if thats good or bad.... :o

sonny
08-03-2009, 09:12 PM
Hope all is well

Thanks for sticking with me on this, also I'm sure a lot of other people out
there would like to know how to do this as well.


Please excuse me if the question seems silly
You mean use my include_path and full path to test.php?

like this
my $php_output = `/usr/local/lib/php $ENV{DOCUMENT_ROOT}/test.php`;
I don't have command line access to do a whereis php


I feel as close as, I've ever been to solving this
Thanks
Sonny

KevinADC
08-03-2009, 10:32 PM
run phpinfo and the output should show your path to php:

<? phpinfo(); ?>

sonny
08-04-2009, 01:41 AM
FishMonger

Can you post your 2 test scripts exactly as you have them?
I have no idea why I cannot get my test to work,

Please post them maybe if I see it, I'll get this once and for all

Thanks
Sonny

KevinADC
08-04-2009, 02:35 AM
FishMonger

Can you post your 2 test scripts exactly as you have them?
I have no idea why I cannot get my test to work,

Please post them maybe if I see it, I'll get this once and for all

Thanks
Sonny

Are you testing on a public server or a dedicated server or a local sever? If its a public server with shared hosted accounts then it may not work. I can't get it to work on my shared public server, didn't try on my local server because I assume I could get it to work just like Fish.

FishMonger
08-04-2009, 02:36 AM
I already did post them, see post #18. The followup posts after that show veriations of the my $php_output = `php test.php`; assignment that I tested, and all of them worked for me.

FishMonger
08-04-2009, 02:42 AM
If you can't get that test working, then we'll probably need to use a different method of executing the php script that allows for better error handling.

However, my recommendation would be to drop the php script and do everything in Perl.

sonny
08-04-2009, 03:28 AM
Are you testing on a public server or a dedicated server or a local sever? If its a public server with shared hosted accounts then it may not work. I can't get it to work on my shared public server, didn't try on my local server because I assume I could get it to work just like Fish.

Shared hosting, I give up I will change that script over to php, I knew when I posted this question, it was going to be a long shot.

Look at this tread, and all I wanted to do was display some text from a php file

not to mention it being one of the most basic pages you could ever ask to test with. is there a pro out there that could do this, I
have no doubt they could, but by the time I find one I will be to old to implement it.

Sonny

sonny
08-04-2009, 03:36 AM
If you can't get that test working, then we'll probably need to use a different method of executing the php script that allows for better error handling.

However, my recommendation would be to drop the php script and do everything in Perl.


Thanks for hanging in there with me, this script is not worth anymore time
from anybody, I'm switching it over to php, what I need it to do requires
a dedicated server and a whole ton of other stuff I know nothing about.

Thanks
Sonny

FishMonger
08-04-2009, 03:59 AM
The problem is not with either of the scripts. I'm willing to bet that if you reverse the process and try to do the exact same thing with php, you'll have the same problem.

The problem is with your hosting provider.

sonny
08-04-2009, 04:21 AM
The problem is not with either of the scripts. I'm willing to bet that if you reverse the process and try to do the exact same thing with php, you'll have the same problem.

The problem is with your hosting provider.


Not necessarily, this executes every perl script I use from a php page,
This 100% works I know for a fact in any php page on my site.

exec('perl /home/content/cgi-bin/test.cgi);

Sonny

FishMonger
08-04-2009, 04:40 AM
Then the only possible cause I can think of is either not specifying or specifying the wrong path to the php interpreter. As I've already shown/proven the Perl test script works as written.

We really won't find the cause/solution unless you're willing to add error handling. If you want to switch to a full php solution, then that's perfectly fine but, whatever you decide, don't put the blame for this failure on Perl.

sonny
08-04-2009, 04:49 AM
Then the only possible cause I can think of is either not specifying or specifying the wrong path to the php interpreter. As I've already shown/proven the Perl test script works as written.

We really won't find the cause/solution unless you're willing to add error handling. If you want to switch to a full php solution, then that's perfectly fine but, whatever you decide, don't put the blame for this failure on Perl.

OK I know how to add it the php page,
could you show me how to add error handing to test.cgi?

Thanks
Ed

KevinADC
08-04-2009, 05:19 AM
It doesn't work on my shared public host either, no matter what paths to php or the php script I try, and I tried them all. I also tried modifying the $ENV{PATH} settings with no luck. I have a feeling its for security that shared hosts don't allow it. They probably have PHP disabled from processing command line requests.

FishMonger
08-04-2009, 05:27 AM
It doesn't work on my shared public host either, no matter what paths to php or the php script I try, and I tried them all. I also tried modifying the $ENV{PATH} settings with no luck. I have a feeling its for security that shared hosts don't allow it. They probably have PHP disabled from processing command line requests.

That may be true and if it is then they still have a big security hole because they allow the reverse i.e., running a Perl script from php and as you know, Perl is more powerful and due to all of the script kiddies and marketing, php is now more popular for web programming and that's a dangerous combination.

sonny
08-04-2009, 05:29 AM
It doesn't work on my shared public host either, no matter what paths to php or the php script I try, and I tried them all. I also tried modifying the $ENV{PATH} settings with no luck. I have a feeling its for security that shared hosts don't allow it. They probably have PHP disabled from processing command line requests.

Safety is the most important thing anyway, I'll just stay with JS and a image
tag for noscript for now, no one can say, we didn't give it a really good try.

Thanks to the both of you, and KevinADC you hung in there all day as well.
can you imagine if we would have pulled this off, there are tons of people out
there looking to do something like this.

Thanks again
Sonny

sonny
08-04-2009, 05:31 AM
That may be true and if it is then they still have a big security hole because they allow the reverse i.e., running a Perl script from php and as you know, Perl is more powerful and due to all of the script kiddies and marketing, php is now more popular for web programming and that's a dangerous combination.

You hit that on the nose,
Sonny

KevinADC
08-04-2009, 05:35 AM
This works but its not really the same thing:



#!/usr/bin/perl
use LWP::Simple;

$php_output = get('../test.php');
print "Content-type: text/html\n\n";
print <<ENDOFHTML;
<html><head><title>Hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
ENDOFHTML

print "<span style=\"color: red\">$php_output</span></body></html>";

KevinADC
08-04-2009, 05:40 AM
That may be true and if it is then they still have a big security hole because they allow the reverse i.e., running a Perl script from php and as you know, Perl is more powerful and due to all of the script kiddies and marketing, php is now more popular for web programming and that's a dangerous combination.

I'm familiar with the basics but all the security implications are out of my range of experience and knowledge. It seems to me that PHP has its hooks sunk into the server and mysql and other things that perl has no direct hooks into. But I could be totally wrong.

sonny
08-04-2009, 05:49 AM
This works but its not really the same thing:



#!/usr/bin/perl
use LWP::Simple;

$php_output = get('../test.php');
print "Content-type: text/html\n\n";
print <<ENDOFHTML;
<html><head><title>Hello</title></head>
<body> <table width="100%">
<tr><td align='center'><h2>Hello this is a Test</h2></td></tr></table><br />
ENDOFHTML

print "<span style=\"color: red\">$php_output</span></body></html>";


No what, I can do some things with that,
Thanks

do you think putting "use LWP::Simple;"
in my cgi scripts can cause any security concerns? other then that
concern I like it.

RESOLVED (how do I officially tag this tread as resolved?)

KevinADC
08-04-2009, 05:58 AM
There are no security problems with LWP::Simple in the perl script. But if you need to pass paramaters to the php script you might need to use something more robust, LWP::UserAgent might work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum