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

how to tell CLI or webserver execution?

P: n/a
Within a php file, how can you tell if it's being called by the
webserver, or by the cli?

I have some pages that I developed to be served, but now we want to
run some of them as cron jobs. I have to change some of the path
references ( our cron job scripts reference include files in the
webserver directory, and relative paths in the include files break
when they are called by a script in another directory ), and to make
everything more interoperable, I need to reference things based on
whether it's being called by Apache or by the cli. Specifically, I'm
making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
called by the cli.

So, I need to know if the script is being called by apache or the
command line, so I can choose how to references certain included
files. How should I do this?
Oct 24 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 24 Okt., 17:29, lawpoop <lawp...@gmail.comwrote:
Within a php file, how can you tell if it's being called by the
webserver, or by the cli?

I have some pages that I developed to be served, but now we want to
run some of them as cron jobs. I have to change some of the path
references ( our cron job scripts reference include files in the
webserver directory, and relative paths in the include files break
when they are called by a script in another directory ), and to make
everything more interoperable, I need to reference things based on
whether it's being called by Apache or by the cli. Specifically, I'm
making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
called by the cli.

So, I need to know if the script is being called by apache or the
command line, so I can choose how to references certain included
files. How should I do this?
You gave the answer yourself:

if (empty($_SERVER['DOCUMENT_ROOT']))
{
}

There certainly are other ways - but why bother?

greetings,
Phil
Oct 24 '08 #2

P: n/a
lawpoop wrote:
Within a php file, how can you tell if it's being called by the
webserver, or by the cli?

I have some pages that I developed to be served, but now we want to
run some of them as cron jobs. I have to change some of the path
references ( our cron job scripts reference include files in the
webserver directory, and relative paths in the include files break
when they are called by a script in another directory ), and to make
everything more interoperable, I need to reference things based on
whether it's being called by Apache or by the cli. Specifically, I'm
making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
called by the cli.

So, I need to know if the script is being called by apache or the
command line, so I can choose how to references certain included
files. How should I do this?
One way would be to see if something webserver specific is set, i.e.

if (isset($_SERVER['SERVER_ADDR'])) {

This should be true if running under a webserver, and false if not. Of
course there are several other values you could use, also.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 24 '08 #3

P: n/a
On Oct 24, 11:58*am, "atpu...@punktat.de" <atpu...@googlemail.com>
wrote:
>
You gave the answer yourself:

if (empty($_SERVER['DOCUMENT_ROOT']))
{

}

There certainly are other ways - but why bother?
Thanks, Phil. I just didn't know if this was the "for sure" answer.
Perhaps under certain circumstances or configurations, this might have
a value when php is called from the cli. I didn't know.

Oct 24 '08 #4

P: n/a
On Oct 24, 12:03*pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
One way would be to see if something webserver specific is set, i.e.

if (isset($_SERVER['SERVER_ADDR'])) {

This should be true if running under a webserver, and false if not. *Of
course there are several other values you could use, also.
Thanks, Jerry. I just wasn't sure if certain variables were more
reliable than others. I suppose SERVER_ADDR couldn't exist in the cli;
assuming it means "web sever address".

I just checked out the php manual, and it says: 'SERVER_ADDR' The IP
address of the server under which the current script is executing ...
which doesn't necessarily rule out an ip address for the server when
called from cli... :P

Oct 24 '08 #5

P: n/a
On Oct 24, 11:29 am, lawpoop <lawp...@gmail.comwrote:
Within a php file, how can you tell if it's being called by the
webserver, or by the cli?

I have some pages that I developed to be served, but now we want to
run some of them as cron jobs. I have to change some of the path
references ( our cron job scripts reference include files in the
webserver directory, and relative paths in the include files break
when they are called by a script in another directory ), and to make
everything more interoperable, I need to reference things based on
whether it's being called by Apache or by the cli. Specifically, I'm
making a reference to $_SERVER['DOCUMENT_ROOT'], which is empty when
called by the cli.

So, I need to know if the script is being called by apache or the
command line, so I can choose how to references certain included
files. How should I do this?

You mean you're creating an if() statement that references one path if
the script is called in the web environment, but another path if it is
not? Personally I'd rather move the necessary files to some central
include file, something in the PHP include path. That way you won't
have to change your if() statement, should the structure of the
server, or the site, ever change.

Oct 24 '08 #6

P: n/a
lawpoop wrote:
On Oct 24, 12:03 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>One way would be to see if something webserver specific is set, i.e.

if (isset($_SERVER['SERVER_ADDR'])) {

This should be true if running under a webserver, and false if not. Of
course there are several other values you could use, also.

Thanks, Jerry. I just wasn't sure if certain variables were more
reliable than others. I suppose SERVER_ADDR couldn't exist in the cli;
assuming it means "web sever address".

I just checked out the php manual, and it says: 'SERVER_ADDR' The IP
address of the server under which the current script is executing ...
which doesn't necessarily rule out an ip address for the server when
called from cli... :P

Actually, it does. You always have at least two ports on any internet
connected computer - the external IP address and 127.0.0.1. But you
aren't accessing the script via an IP address, so neither one is valid.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 25 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.