tags:

views:

84

answers:

6

Hi--

I'm writing a PHP script that I want to disable from web access (I will ask users to move it out of the web root and execute via CLI, but you never know if they'll listen!)

Is there a simple function that occurs to anyone to make the page die if it's requested by a browser?

Thanks for any ideas.

+5  A: 

You could test whether the script is being run through the CLI using php_sapi_name().

It can return a whole bunch of different possible values when run on a HTTP server - difficult to make a reliable distinction there - but there seems to be only one possible return value for the CLI: cli.

If you're looking for an all-purpose solution, make sure you read the comment thread below for more detailed discussion on some potential gotchas.

Pekka
Just check the warning by *michal at roszka dot pl* on the [php_sapi_name()](http://php.net/manual/en/function.php-sapi-name.php) page. I remember hitting problems with this before (it was a long time ago though, so I can't be specific). I ended up using [$_SERVER](http://php.net/manual/en/reserved.variables.server.php), as it produces markedly different output when run as CLI. I *think* checking for the existence of `$_SERVER['argv']` is enough.
Mike
@Mike good point and important gotcha! I, on the other hand, have had similar problems with `$_SERVER[]` checks in the past - one time failing the CLI test on a production server that worked on the test servers. I can't say which array key I checked for back then though, I'd have to look it up. The absence of a `$_SERVER["REMOTE_ADDR"]` might be an indicator as well - but I'm sure there will be some exotic server not passing it through in http mode either. Arrrghhh.... :)
Pekka
thanks pekka, mark, user257493 and all for the comments. I'm going with this, assuming it'll work in the majority of cases. I appreciate the discussion and great explanations!
julio
A: 

You can use php-sapi-name function to detect if script was requested by web server or cli interface.

Michał Pipa
+1  A: 

I'm not a PHP expert but you could check the $_SERVER variable ?

slurdge
A: 
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) != 'cli') {
    die "You are not using CLI".PHP_EOL;
}
Mark Baker
A: 

you can use php-sapi-name or you can use the predefined constant which is marginally faster (although you'd never notice!)

<?php
if(PHP_SAPI != 'cli') exit;
// continue
nathan
This has potential problems discussed in the comments to my answer.
Pekka
it has 0 problems as you note yourself 'but there seems to be only one possible return value for the CLI: cli' ;)
nathan
@nathan nope, read @Mike's comment, it's not 100% true, it could be `cgi` or `fgci` in some very rare cases. It's probably worth the nit-picking because it sounds like the OP wants a general-purpose solution. But in normal cases, I agree it will be good enough.
Pekka
well noted! i concede and concur +1 to you're answer
nathan
+4  A: 

'PHP_SELF' The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The FILE constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.

http://www.php.net/manual/en/reserved.variables.server.php

Hey, nice! I like this, and it circumvents the CLI/CGI problem outlined in the comments to my answer. One would have to check whether it's a path or a URL. Can't see any gotchas with this - differing opinions, anyone? +1
Pekka