tags:

views:

46

answers:

3

Hi,

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,

+1  A: 
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
+4  A: 

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.

stereofrog
I think you got it right
Pekka