• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

PHP code from a CGI script?

Has anyone been able to sucessfully generate PHP code from a CGI script
and have it processed by PHP before its given to the browser?

For example, I have PHP, Perl CGI scripts, Apache and a MySQL database all
working fine under Linux.  When I run a CGI script from a web browser, it
outputs some stuff including some PHP code.  But the result is not like
it would be from a normal PHP phtml document - I can see the PHP code
in the browser because its not processed by PHP as its outputed from
the perl script.  Any ideas?

### Print Header ###
printf "Content-type: text/html\n\n";
printf "<head><title>test</title></head>";
printf "<body>";

printf "
<?
echo \"testing from PHP\";
?>
";

printf "
</body>";

I actually see the <? echo... ?> in the HTML source from the browser.  
Why? I want it processed by PHP first where I should see
"testing from PHP" on the web page.  Note: This exact PHP code
works fine from a normal HTML doc such as test.html, but not
when its generated from a CGI script like this.

Thanks.
0
scottg
Asked:
scottg
1 Solution
 
jcondeCommented:
Scottg:

What you're trying to do will not work at all!
here's whats going on:

In order for php to work, you MUST have defined an extension for the phtml file.  In other words, the php interpreter will not be activated unless the extension is previously defined in your httpd.conf file (That is if php is compiled into apache as a module).

What you need to do is the following:

### Print Header ###
    printf ("Content-type: text/html\n\n");
    printf ("<head><title>test</title></head>)";
    printf ("<body>");
    f = fopen("php-script.php3","w");
    fprintf (f,"<? echo \"testing from php\"; ?>");
    fclose (f);
    system ("/bin/php php-script.php3 > out.out");
    f = fopen("out.out","r");
    while (!feof(f))
      {
        fgets (line,100,f);
        puts (line);
      }
    fclose (f);

   As you can see, you will be working alot!!!

Can't you just use php for youre application?

Or, what about just using a cgi??

jorge
0
 
scottgAuthor Commented:
Thanks for your reponse.

I do have "AddType application/x-httpd-php3 .html" in my httpd.conf and it works fine normally.  

>Can't you just use php for youre application?

Ya, I probably can, but I'm not smart enough(with PHP).  I can understand how it would work with the CGI, but not PHP alone.

Do you have any examples?  I essentially have a form that I want to allow the user to search through a MySql database.  Its simple enough, I just need a jump start with a short example.  Maybe straight CGI is better?  E-Mail me at scottg@niagara.com if you have any code or additional thoughts.

Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now