Solved

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

Posted on 2010-08-16
6
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 29

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 29

Expert Comment

by:chilternPC
ID: 33448829
@davbladwin - PHP is now becoming popular to run  command line as well as on a server
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 29

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

630 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