Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a PHP file run-able only through CLI mode?

It's available to browser,

but I don't want it to execute when browsed by user,

say,when browsed should exit,

is there a way to detect whether it's currently Cmmand Line Mode?

like image 615
omg Avatar asked Jun 02 '09 23:06

omg


People also ask

What is CLI mode PHP?

PHP CLI is a short for PHP Command Line Interface. As the name implies, this is a way of using PHP in the system command line. Or by other words it is a way of running PHP Scripts that aren't on a web server (such as Apache web server or Microsoft IIS). People usually treat PHP as web development, server side tool.

How do I pass a command line argument to a PHP script?

To pass command line arguments to the script, we simply put them right after the script name like so... Note that the 0th argument is the name of the PHP script that is run. The rest of the array are the values passed in on the command line. The values are accessed via the $argv array.


2 Answers

See:

  • What is the canonical way to determine commandline vs. http execution of a PHP script?
  • Is there any way to know if a php script is running in cli mode?
  • PHP - what is the best & easy way to determine if the current invocation is from CLI or browser

Short story: php_sapi_name().

like image 125
Paolo Bergantino Avatar answered Oct 07 '22 00:10

Paolo Bergantino


Here is what i'm using, for a long time now... (since php 4 iirc)

(PHP_SAPI !== 'cli' || isset($_SERVER['HTTP_USER_AGENT'])) && die('cli only');

to be used as first line of the php script.

like image 33
Florian Fida Avatar answered Oct 06 '22 23:10

Florian Fida