473,473 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

CLI/web test

Hi,

Is there a recognised (ie. reliable) way for a script to test
if it is being run from the command line or through a web server ?

My guess whould have been :-

if (isset($_SERVER))

THEN it is being run from a web server.

But this may also be set when run through the CLI ?

Any suggestions ?

Thanks,
John.

Jul 17 '05 #1
4 1611
John wrote:
My guess whould have been :-

if (isset($_SERVER))

THEN it is being run from a web server.

But this may also be set when run through the CLI ?


http://www.php.net/php_sapi_name returns 'cli' when the script is invoked on
the command line.
JW

Jul 17 '05 #2
John wrote:
Is there a recognised (ie. reliable) way for a script to test
if it is being run from the command line or through a web server ?
Try
http://www.php.net/php_sapi_name
My guess whould have been :-

if (isset($_SERVER))

THEN it is being run from a web server.

But this may also be set when run through the CLI ?


Yes, for me $_SERVER is set both for PHP CLI and PHP as a Apache module;
however its contents are different.

for example:
$_SERVER['SERVER_ADDR'] only exists for the module
$_SERVER['argc'] only exists for the CLI

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #3
Janwillem Borleffs wrote:
John wrote:
My guess whould have been :-

if (isset($_SERVER))

THEN it is being run from a web server.

But this may also be set when run through the CLI ?

http://www.php.net/php_sapi_name returns 'cli' when the script is invoked on
the command line.


Thanks - I find that it returns 'cgi', but that will do.

John.

Jul 17 '05 #4
John wrote:
Janwillem Borleffs wrote:
John wrote:
My guess whould have been :-

if (isset($_SERVER))

THEN it is being run from a web server.

But this may also be set when run through the CLI ?

http://www.php.net/php_sapi_name returns 'cli' when the script is
invoked on the command line.

Thanks - I find that it returns 'cgi', but that will do.

John.

It probably depends on if you're using the CGI build or CLI build, as
both exist and both work slightly differently.

What I would do is simply test for a web-environment variable such as
$_SERVER['REQUEST_URI'].

if (isset($_SERVER['REQUEST_URI'])){
This is a web instance
}
else {
This is a CLI instance.
}
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
4
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and...
0
by: Andrea M. Segovia | last post by:
I just compiled (but did not install) perl 5.8.0 on an SGI Origin 300 server (IP35) running IRIX 6.5.20m. Make test reported one test error, which I narrowed down to .../lib/ExUtils/t/Constant.t...
0
by: Jussi Mononen | last post by:
Hi, I'm having problems to successfully execute the test scripts on a Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up with the following error message "PARI: *** ...
4
by: arotem | last post by:
Hi, I am trying to call an unbound method (PrintInput) with the object instance as the first argument but getting the following error: "TypeError: unbound method PrintInput() must be called with...
0
by: Tim Haughton | last post by:
I've just released an article on using Test Driven Development with C# and Windows Forms. GUI's are often difficult to test, so I thought it might be of interest. The article along with the...
6
by: Ben Finney | last post by:
Howdy all, Summary: I'm looking for idioms in unit tests for factoring out repetitive iteration over test data. I explain my current practice, and why it's unsatisfactory. When following...
27
by: Josh | last post by:
We have a program written in VB6 (over 100,000 lines of code and 230 UI screens) that we want to get out of VB and into a better language. The program is over 10 years old and has already been...
6
by: ypjofficial | last post by:
HI, I have following terrific confusion. class test { public: int i; int * j; int **k;
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.