Solved

PHP code from a CGI script?

Posted on 1998-08-06
2
231 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 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