upperbid Posted September 26, 2009 Share Posted September 26, 2009 I am wanting to run some php code within a page with a .cgi extension on a site that runs with perl. I have server access. How can I do this? Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/175594-php-in-perl-cgi-script-how/ Share on other sites More sharing options...
Handy PHP Posted September 26, 2009 Share Posted September 26, 2009 Well, you can try this: <Location /index.html> AddType application/x-httpd-php .cgi </Location> But I don't think it will work. It is likely that the server will only parse one language or the other... As a result, it will more than likely try to parse one as the other and give you a fatal error. You really should try to convert the PHP portion of your code to Perl but I assume that you either don't know Perl or the PHP script is too large to convert. Of course, it is possible to run the PHP script remotely and dump the data back into the Perl script but that requires the proper Perl code to be added to do so. I don't know Perl well enough to tell you how to go about that. Handy PHP Quote Link to comment https://forums.phpfreaks.com/topic/175594-php-in-perl-cgi-script-how/#findComment-925531 Share on other sites More sharing options...
Mark Baker Posted September 26, 2009 Share Posted September 26, 2009 I believe a CGI should script should be parsed based on the shebang at the start of the file #!/bin/perl or #!/bin/php Quote Link to comment https://forums.phpfreaks.com/topic/175594-php-in-perl-cgi-script-how/#findComment-925536 Share on other sites More sharing options...
.josh Posted September 27, 2009 Share Posted September 27, 2009 The problem is getting two languages to be parsed in a single file. Off the top of my head I don't think you can really do that, but, perhaps you can get around it by treating it in the same way you would ajax: call the script, the script does the processing and outputs the results as plain text, and on the other end, it is assigned to a var. I don't know perl enough to be more specific with perl syntax, but if we were to look at it the other way around: getting a perl script to be included in a php script, it would go something like this: Let's say you just want it to be executed and aren't expecting anything returned, or else a simple scalar value: <?php $result = file_get_contents('somePerlScript.cgi'); ?> this executes the script and assigns the results to $result. It is "executed" in the same way as if you were to go directly to it in your browser, so $result would contain the same stuff as a rightclick > view source from in your browser. So if somePerlScript.cgi were to do db interaction or whatever and in the end, do like [pre]print 'kitty';[/pre] that would be assigned to $result. If for instance I needed somePerlScript.cgi to set certain variables or whatever, I could have it output the actual code to set the variable, and eval the string, and it will parse the string as if it were php code. I don't know if perl has an equivalent function, though...example: somePerlScript.cgi #!/usr/bin/perl print "content-type: text/html \n\n"; print '$x = 123;'; script.php <?php // somePerlScript.cgi is called and it outputs the following: $x = 123; $result = file_get_contents('somePerlScript.cgi'); // so basically you have the equivalent of this: $result = '$x = 123'; eval($result); // evaluate the string as if it were php code echo $x; // output: 123 ?> So that's basically what you could do, in concept. Just have to do it the other way around, where you are calling a php script in your perl script. Quote Link to comment https://forums.phpfreaks.com/topic/175594-php-in-perl-cgi-script-how/#findComment-925617 Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 Crayon Violent beautiful article, needs really pining man... 10/10 a star. love his examples, just like a real teacher in front of us. Quote Link to comment https://forums.phpfreaks.com/topic/175594-php-in-perl-cgi-script-how/#findComment-925646 Share on other sites More sharing options...
trq Posted September 27, 2009 Share Posted September 27, 2009 So that's basically what you could do, in concept. Just have to do it the other way around, where you are calling a php script in your perl script. All well and good excepting even your php is floored. file_get_contents gets the contents of the file, not the result of a file thats been parsed by a server. Of course, providing the appropriate url wrappers are enabled you could pass file_get_contents a url however and this would get you the results of that scripts execution. To the op, you'll likely want to look at perl's system or exec functions. Quote Link to comment https://forums.phpfreaks.com/topic/175594-php-in-perl-cgi-script-how/#findComment-925816 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.