Veer Shrivastav
Veer Shrivastav

Reputation: 5496

Block direct HTTP access to a PHP File

I have a php script which I want to run only from the command prompt, and want it to be blocked over HTTP. How should I do it?

What I have tried:

define('CLI_SCRIPT', true);

It did not help me a lot, was still able to open the file.

Adding to .htaccess:

DENY FROM ALL

It blocks the file from all source, even from the command prompt.

I cannot place it outside the public_html. I don't have access to other folders of the server.

Upvotes: 1

Views: 170

Answers (1)

T0xicCode
T0xicCode

Reputation: 4911

You can use the function php_sapi_name to exit early if the return value is not "cli".

if (php_sapi_name() !== "cli") {
    exit();
}

Upvotes: 4

Related Questions