473,788 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2976
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*******@attgl obal.net
=============== ===

Oct 24 '08 #3
On Oct 24, 11:58*am, "atpu...@punkta t.de" <atpu...@google mail.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...@attg lobal.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...@attg lobal.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*******@attgl obal.net
=============== ===

Oct 25 '08 #7

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

Similar topics

5
1788
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 the sql engine with your own data types. Can't remember exactly how it worked but you could store html with embedded 'sql code' in the database.
3
1682
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 easy way to tell how my .php file was invoked?
1
2603
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
2795
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 both the iis and sql server in a single machine. Not too long ago, the machine had some hardware problems, and management has decided to purchase new servers, for both asp.net and sql server.
8
2264
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 following 2 examples): ---- EXAMPLE 1 -------- OK --> Dim p As New ProfileCommon OK --> p = Profile.GetProfile(Me.ddlRacer.SelectedItem.Text) FAILS --> Me.lblDebug.Text = p.FirstName.ToString
3
4933
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 has no data, the second report doesn't run. All execution stops. Is there a way in VBA to go on to the next report after the No Data?
2
1182
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. When they click the unlock button A series of If Else statements decides by the I.P. Address which site they are at. From this it calls an appropriate .bat file passing the IP and UserName. The series of .bat files and .vbe files are located...
1
2056
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 webapplication. This proxy is responsible for security aspects (authentication/authorization) that is not handled by the webapplication itself. For that I created a class that implements IHttpHandler and registered it in the webproject's...
1
6929
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 from CGIHTTPServer import CGIHTTPRequestHandler serve = HTTPServer(("",8080),CGIHTTPRequestHandler)
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
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 we have to send another system
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.