Solved

PHP code from a CGI script?

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

762 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

18 Experts available now in Live!

Get 1:1 Help Now