473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

how to tell CLI or webserver execution?

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
6 2958
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Heinz | last post by:
Hi there Out of curiosity .... Long time ago I was working with Infomix (now IBM) Universal Server, that had a data type 'html' implemented as data blade. Through data blades you could extend...
3
by: Steven Stern | last post by:
I have a script that will be run as a cron task every night but occasionally will run when request by the browser. When run by the browser, I want it to output results to the screen. What's an...
1
by: werrt | last post by:
Is it possible to get the event in the servlet when a user logined to the webserver just closes the IE.
12
by: Ann Marinas | last post by:
Hi all, I would like to ask for some help regarding separating the asp.net webserver and the sql server. I have created an asp.net application for a certain company. Initially, we installed...
8
by: VB Programmer | last post by:
I would appreciate your assistance on this ASP.NET 2.0 site.... This is the wierd problem: While accessing the built in .NET functions for 'profiling' or 'membership' an error is generated (see...
3
by: sara | last post by:
Hi - I have a button that runs 2 reports. If there is no data on the report, I use the No Data event, and tell the user, and Cancel the execution of that report. However, if the first report...
2
by: Zach Burnett | last post by:
I have created a page that should unlock our local users throughout our network. The page is simple enough. It just takes users input for the IP Address and the User Name of the lockout computer....
1
by: swissclash79 | last post by:
Good morning everybody I have a question regarding configuring mappings in the development webserver. I am currently implementing a proxy server that is lied between the clients and a customer's...
1
by: mpc | last post by:
hello, how does one run a PHP page with a python webserver? Lets say i have a simple python web server running /path/webserver.py #!/usr/bin/env python from BaseHTTPServer import HTTPServer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.