Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-16
6
Medium Priority
?
250 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 84

Accepted Solution

by:
Dave Baldwin earned 1000 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 29

Expert Comment

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

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

670 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