?
Solved

PHP code from a CGI script?

Posted on 1998-08-06
2
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
jconde earned 390 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 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