1

What would be the cleanest method to tell if PHP has been invoked via a POST for example and not from html (under a web broser)

Thanks,

tru7
  • 6,348
  • 5
  • 35
  • 59
  • possible duplicate of [What is the canonical way to determine commandline vs. http execution of a PHP script?](http://stackoverflow.com/questions/173851/what-is-the-canonical-way-to-determine-commandline-vs-http-execution-of-a-php-sc) – Pekka Sep 30 '10 at 11:30
  • To make things clear ... a POST as in the HTTP/1.1 Method is not the same as CLI ... what do you mean by stand-alone script? – ChrisR Sep 30 '10 at 11:32

3 Answers3

5

If I understand your question correctly, it's not about http vs. command line call, but rather browser vs. "non-browser" (e.g. via curl, wget etc) call. There's no way to check this, because wget etc are technically browsers, they just don't happen to have a GUI. You can try checking HTTP_USER_AGENT, but this is totally unreliable, because there's no way to enforce a client to identify itself correctly.

user187291
  • 53,363
  • 19
  • 95
  • 127
2

This has been answered in another Q: What is the canonical way to determine commandline vs. http execution of a PHP script?

Community
  • 1
  • 1
TuomasR
  • 2,296
  • 18
  • 28
1
function __get_started_from_cli_state()
{
    return substr(php_sapi_name(), 0, 3) == 'cli';
}

function __get_started_from_cgi_state()
{
    return substr(php_sapi_name(), 0, 3) == 'cgi';
}

function __get_started_from_browser_state()
{
    return !(__get_started_from_cli_state() ||
             __get_started_from_cgi_state());
}
Svisstack
  • 16,203
  • 6
  • 66
  • 100