Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP detecting human browser in cron job

Tags:

php

user-agent

I want to prevent users from running my cron job manually. Apart from using an unpredictable filename I want to have some sort of check in code.

Obviously using any clientside headers is a waste of time so I thought the easiest way around this would be to detect the presence of $_SERVER['SERVER_NAME'] which as far as I know is not set in CLI.

Are there better ways of doing this?

like image 601
Andy Avatar asked Feb 22 '26 23:02

Andy


1 Answers

Have a look at php_sapi_name. It returns the type of interface between web server and PHP.

Example:

<?php

if(php_sapi_name() == 'cli') {
  // CLI
}
else {
  // HTTP
}

For more information, and additional examples, have a look at http://www.electrictoolbox.com/determine-php-run-via-http-or-cli/.

like image 78
Francois Deschenes Avatar answered Feb 25 '26 11:02

Francois Deschenes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!