Solved

PHP code from a CGI script?

Posted on 1998-08-06
2
230 Views
Last Modified: 2013-12-25
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
Comment
Question by:scottg
2 Comments
 
LVL 7

Accepted Solution

by:
jconde earned 130 total points
ID: 1831414
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
 

Author Comment

by:scottg
ID: 1831415
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now