By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,722 Members | 1,904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,722 IT Pros & Developers. It's quick & easy.

calling php from a cgi script

P: n/a
As a shell script, the following program works as expected.

---------------------------------------
#! /usr/bin/sh

echo '<html><? print 1 ?></html>' | php
---------------------------------------

However, when run as a CGI script, invoking php causes the php
interpreter to load the current file (ie, the sh script), which
obviously is ga ga.

How can I supress this behavior of interpreting the current file when
invoking PHP in a CGI script?

Thanks,
David
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
David Van Horn wrote:
As a shell script, the following program works as expected.

---------------------------------------
#! /usr/bin/sh

echo '<html><? print 1 ?></html>' | php
---------------------------------------

However, when run as a CGI script, invoking php causes the php
interpreter to load the current file (ie, the sh script), which
obviously is ga ga.

How can I supress this behavior of interpreting the current file when
invoking PHP in a CGI script?

Thanks,
David


and what is the reason you would do this? The whole reason for PHP is
to be able to write HTML and include dynamic elements from the OS (sh
MySQL, etc... this is why PHP has a call interface to the os (shell_exec
and backtick operators (``) to call sh from PHP not the other way
around. I could be wrong, but I think you are embarking down a path
that will cause you much grief in the future.

You want to use the proper tool to achieve the results and using sh to
call PHP is asking the server to start another program to do something
the sh CGI script is already doing -- there is no value PHP can add by
using it in this manner. in fact is more complicated

echo '<htm> 1 </html>';

This uses one program to execute the same thing not 2 and is less
resource intensive.

Michael Austin.
Jul 17 '05 #2

P: n/a
On Wed, 23 Jun 2004, Michael Austin wrote:
David Van Horn wrote:
As a shell script, the following program works as expected.

---------------------------------------
#! /usr/bin/sh

echo '<html><? print 1 ?></html>' | php
---------------------------------------

However, when run as a CGI script, invoking php causes the php
interpreter to load the current file (ie, the sh script), which
obviously is ga ga.

How can I supress this behavior of interpreting the current file when
invoking PHP in a CGI script?

Thanks,
David


<clip>

echo '<htm> 1 </html>';

This uses one program to execute the same thing not 2 and is less
resource intensive.


obviously the example is serving as an example and not what this person
really wants to accomplish. short, precise questions should be
welcomed<http://www.catb.org/~esr/faqs/smart-questions.html#volume>.

it seems there doesn't exist an option to shut off PHP's obsession of
acting with such a large ego.

i can't find in the PHP C code where the interpreter decides to behave
differently depending if it is operating in a HTTP/CGI environment or in a
shell environment at a command-line terminal.

i tried creating an environment for PHP with the env command, but this
only keeps PHP from processing the file (for which it gives a "No input
file specified." error), and doesn't keep it from stealing the show and
responding to the HTTP/CGI request on its own. can't PHP tell something
is waiting at the door on standard input (stdin) and proces it?

---
#!/bin/sh

echo 'Content-type: text/plain'
echo ''
echo '<html><?php echo 1; ?></html>' | (env SCRIPT_FILENAME='' php)
---

/a
Jul 17 '05 #3

P: n/a
Aaron S. Hawley wrote:
On Wed, 23 Jun 2004, Michael Austin wrote:

David Van Horn wrote:

As a shell script, the following program works as expected.

---------------------------------------
#! /usr/bin/sh

echo '<html><? print 1 ?></html>' | php
---------------------------------------

However, when run as a CGI script, invoking php causes the php
interpreter to load the current file (ie, the sh script), which
obviously is ga ga.

How can I supress this behavior of interpreting the current file when
invoking PHP in a CGI script?

Thanks,
David


<clip>

echo '<htm> 1 </html>';

This uses one program to execute the same thing not 2 and is less
resource intensive.

obviously the example is serving as an example and not what this person
really wants to accomplish. short, precise questions should be
welcomed<http://www.catb.org/~esr/faqs/smart-questions.html#volume>.

it seems there doesn't exist an option to shut off PHP's obsession of
acting with such a large ego.

i can't find in the PHP C code where the interpreter decides to behave
differently depending if it is operating in a HTTP/CGI environment or in a
shell environment at a command-line terminal.

i tried creating an environment for PHP with the env command, but this
only keeps PHP from processing the file (for which it gives a "No input
file specified." error), and doesn't keep it from stealing the show and
responding to the HTTP/CGI request on its own. can't PHP tell something
is waiting at the door on standard input (stdin) and proces it?

---
#!/bin/sh

echo 'Content-type: text/plain'
echo ''
echo '<html><?php echo 1; ?></html>' | (env SCRIPT_FILENAME='' php)
---

/a

What version are you using, because according to the docs, versions
prior to 4.2.0 could not be used interactively (4.2.0 was experimental
only and 4.3.0 removed the experimental status -- see
http://www.php.net/features.commandline

So, to see where the problem lies, break it down to it's basic parts.

what happens when you type:
php -v
from the command line?

Just because a binary can do things from a web server does not imply
that you can also use it from the command line (CLI) - as you/he are/is
attempting to do.

Quite frankly, you could be opening up a security hole big enough to
drive a semi-truck through... I understood what he was displaying was a
"sample" - and from my perspective, a very inefficient coding practice.
There is no added value of using PHP to do what shell/cgi was already
doing - echoing text back to stdout.
Michael Austin.
Jul 17 '05 #4

P: n/a

On Wed, 23 Jun 2004, Michael Austin wrote:
Aaron S. Hawley wrote:
<clip>

it seems there doesn't exist an option to shut off PHP's obsession of
acting with such a large ego.

i can't find in the PHP C code where the interpreter decides to behave
differently depending if it is operating in a HTTP/CGI environment or in a
shell environment at a command-line terminal.

i tried creating an environment for PHP with the env command, but this
only keeps PHP from processing the file (for which it gives a "No input
file specified." error), and doesn't keep it from stealing the show and
responding to the HTTP/CGI request on its own. can't PHP tell something
is waiting at the door on standard input (stdin) and proces it?

---
#!/bin/sh

echo 'Content-type: text/plain'
echo ''
echo '<html><?php echo 1; ?></html>' | (env SCRIPT_FILENAME='' php)
---

/a

What version are you using, because according to the docs, versions
prior to 4.2.0 could not be used interactively (4.2.0 was experimental
only and 4.3.0 removed the experimental status -- see
http://www.php.net/features.commandline

So, to see where the problem lies, break it down to it's basic parts.


brilliant.
what happens when you type:
php -v
from the command line?
I was running my example in 4.36:

PHP 4.3.6 (cgi) (built: Apr 21 2004 01:44:44)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

So it's still broken.
Just because a binary can do things from a web server does not imply
that you can also use it from the command line (CLI) - as you/he are/is
attempting to do.

<snip>

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.