Solved

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

Posted on 2010-08-16
6
243 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from 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.

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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