Solved

How does my PHP script know if it is run by a browser or command line?

Posted on 2010-08-16
6
242 Views
Last Modified: 2012-05-10
I want my php script to know if it is run by the browser or by the command line.

How do I tell the difference?

argv==NULL means browser?
0
Comment
Question by:pmsguy
  • 3
  • 2
6 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 33448774
Good question.  PHP is run by the server by the way.  The main difference is the user that PHP runs under.  If it is sent to the browser from the web server, then it runs under the permissions of the web server which are usually very restrictive.  On the command line, it runs under your user permissions.

Here's the page http://us3.php.net/manual/en/function.get-current-user.php for "get_current_user()".  Technically, it should return the name of user whose permissions you are running under, not the name of the 'owner'.  With a test case or two, you should be able to get the info you want.  This page says that 'argv' can also contain the 'GET' query string: http://us3.php.net/manual/en/reserved.variables.server.php
0
 
LVL 28

Assisted Solution

by:chilternPC
chilternPC earned 250 total points
ID: 33448816
The following simple function returns a true if the script is run from a command prompt and false otherwise.

<?php
 
function isCli() {
 
     if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
          return true;
     } else {
          return false;
     }
}
 
?>
0
 
LVL 28

Expert Comment

by:chilternPC
ID: 33448829
@davbladwin - PHP is now becoming popular to run  command line as well as on a server
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:chilternPC
ID: 33448844
Using PHP from the commnad line
http://php.net/manual/en/features.commandline.php
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33448935
@chilternPC, yes i know.  I have some text processing utilities running PHP from the command line on this computer.  @pmsguy just wants to be able to tell the difference.
0
 
LVL 3

Expert Comment

by:dockhand
ID: 33448990
It is a matter of symantics, but what I think DaveBaldwin is trying to say that no matter how it is envoked Php is what would be condidered Server Side in a Client-Server app.  Even if envoked by command line it is running on the server and merely feeding the client.

What I think pmsguy is wanting to know is how was the server side code envoked?  You guys both  have already given him tools to ascertain this information.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now