• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

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

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
pmsguy
Asked:
pmsguy
  • 3
  • 2
2 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
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
 
chilternPCCommented:
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
 
chilternPCCommented:
@davbladwin - PHP is now becoming popular to run  command line as well as on a server
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
chilternPCCommented:
Using PHP from the commnad line
http://php.net/manual/en/features.commandline.php
0
 
Dave BaldwinFixer of ProblemsCommented:
@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
 
dockhandCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now