FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Detecting Redirected Output
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Detecting Redirected Output [message #174109] Sun, 22 May 2011 01:06 Go to next message
Bob Smith is currently offline  Bob Smith
Messages: 11
Registered: October 2010
Karma: 0
Junior Member
Running under Win7, a script I'm writing needs to vary its behavior
depending upon whether or not the output from PHP is being redirected.
That is, I want to know from within the PHP script whether it's called as

php foo.php

or

php foo.php > outfile

None of the Output Control Functions appears to be helpful.

Way back in DOS days, there was an IOCTL call which the program could
use to distinguish those two cases. Is there anything comparable in PHP?

--
_________________________________________
Bob Smith -- bsmith(at)sudleydeplacespam(dot)com

To reply to me directly, delete "despam".
Re: Detecting Redirected Output [message #174111 is a reply to message #174109] Sun, 22 May 2011 03:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/21/2011 9:06 PM, Bob Smith wrote:
> Running under Win7, a script I'm writing needs to vary its behavior
> depending upon whether or not the output from PHP is being redirected.
> That is, I want to know from within the PHP script whether it's called as
>
> php foo.php
>
> or
>
> php foo.php > outfile
>
> None of the Output Control Functions appears to be helpful.
>
> Way back in DOS days, there was an IOCTL call which the program could
> use to distinguish those two cases. Is there anything comparable in PHP?
>

Nope. Not possible in PHP. And it shouldn't be, either. The purpose
of a redirect is to be able to save the output.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Detecting Redirected Output [message #174114 is a reply to message #174109] Sun, 22 May 2011 11:06 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Bob Smith wrote:

> Running under Win7, a script I'm writing needs to vary its behavior
> depending upon whether or not the output from PHP is being redirected.
> That is, I want to know from within the PHP script whether it's called as
>
> php foo.php
>
> or
>
> php foo.php > outfile
>
> None of the Output Control Functions appears to be helpful.
>
> Way back in DOS days, there was an IOCTL call which the program could
> use to distinguish those two cases. Is there anything comparable in PHP?

In PHP, yes; but not on Windows, as the POSIX extension is not available
there (according to the manual):

<http://lmgtfy.com/?q=php+cli+redirection+-http>


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Re: Detecting Redirected Output [message #174117 is a reply to message #174111] Sun, 22 May 2011 13:04 Go to previous messageGo to next message
Twayne is currently offline  Twayne
Messages: 135
Registered: September 2010
Karma: 0
Senior Member
In news:ir9v2l$rnv$2(at)dont-email(dot)me,
Jerry Stuckle <jstucklex(at)attglobal(dot)net> typed:
> On 5/21/2011 9:06 PM, Bob Smith wrote:
>> Running under Win7, a script I'm writing needs to vary
>> its behavior depending upon whether or not the output
>> from PHP is being redirected. That is, I want to know
>> from within the PHP script whether it's called as php foo.php
>>
>> or
>>
>> php foo.php > outfile
>>
>> None of the Output Control Functions appears to be
>> helpful. Way back in DOS days, there was an IOCTL call which the
>> program could use to distinguish those two cases. Is
>> there anything comparable in PHP?
>
> Nope. Not possible in PHP. And it shouldn't be, either.
> The purpose of a redirect is to be able to save the
> output.

So says the exalted high troll of the newsgroups whose only intent in life
is to issue meaningless and uncalled for criticisms in order to make up for
his own lack of power by stealing it from others.
Re: Detecting Redirected Output [message #174123 is a reply to message #174114] Sun, 22 May 2011 16:17 Go to previous message
Bob Smith is currently offline  Bob Smith
Messages: 11
Registered: October 2010
Karma: 0
Junior Member
On 5/22/2011 7:06 AM, Thomas 'PointedEars' Lahn wrote:
> Bob Smith wrote:
>
>> Running under Win7, a script I'm writing needs to vary its behavior
>> depending upon whether or not the output from PHP is being redirected.
>> That is, I want to know from within the PHP script whether it's called as
>>
>> php foo.php
>>
>> or
>>
>> php foo.php> outfile
>>
>> None of the Output Control Functions appears to be helpful.
>>
>> Way back in DOS days, there was an IOCTL call which the program could
>> use to distinguish those two cases. Is there anything comparable in PHP?
>
> In PHP, yes; but not on Windows, as the POSIX extension is not available
> there (according to the manual):
>
> <http://lmgtfy.com/?q=php+cli+redirection+-http>

Having just learned that this is trivial in Python, I looked more
carefully at PHP on Windows and found a couple of ways to tell if STDOUT
has been redirected:

1. $fd = fopen ('php://stdout', "w");
if (ftell ($fd) === false)
echo "STDOUT not redirected.\n";
else
echo "STDOUT redirected.\n";
fclose ($fd);

2. $fd = fopen ('php://stdout', "w");
$a = fstat ($fd);
if ($a['dev'] != 0) // or ['rdev']
echo "STDOUT not redirected.\n";
else
echo "STDOUT redirected.\n";
fclose ($fd);

There also is a difference in $a['mode'] which I did not pursue.

--
_________________________________________
Bob Smith -- bsmith(at)sudleydeplacespam(dot)com

To reply to me directly, delete "despam".
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: populate pulldownbox from query
Next Topic: You have an error in your SQL syntax;
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 20:19:05 GMT 2024

Total time taken to generate the page: 0.07736 seconds