I have seen a lot of posts about cron and ZF, but most solutions leave the work available for launch by the public.
What if you want to configure an action that can only be performed by cron? Not some anonymous user, but not someone who should be logged in?
The solution I'm using is to put the file outside of my web root, since it loads ZF enough to use what I need (for example, I don't need the view) and then delete it from cron. My questions is, is this “best practice” for this? What if you need to make the code available over the Internet, but still need to prevent random users from finding and running it?
To illustrate, here is what I do (what works) to do the cron job from the php command line and on the same server, something like this:
* 10 * * * php /Apps/ZF/cronjobs/crontest.php
Webroot: /Apps/ZF/someproject/
crontest.php:
<?php ini_set('include_path', ini_get('include_path') . ':/Apps/ZF/someproject/library'); define('APPLICATION_PATH','/Apps/ZF/someproject/application'); define('APPLICATION_ENVIRONMENT','test'); //Include the loader (for loading ZF resources) require_once 'Zend/Loader.php'; //Include the model (to access the Sites model in this case) require_once(APPLICATION_PATH . '/models/Planets.php'); Zend_Loader::registerAutoload(); $configuration = new Zend_Config_Ini( APPLICATION_PATH . '/config/config.ini', APPLICATION_ENVIRONMENT ); // DB adapter $dbAdapter = Zend_Db::factory($configuration->database); // DB table setup Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); // Whatever code we want to run... $test = new Model_Planets(); $test->fetchEntries(); Zend_Debug::dump($test); ?>
So, as I said, this works, so I'm not looking for someone to write me a solution ... just curious to make it "better." Besides, what if I needed it to be available over the Internet, but still want it to be available only cron? How to make it more flexible (because here I hardcode a few paths that I suspect can be made more dynamic)?
I assume that I can make a list of allowed servers and then check that with $_SERVER['REMOTE_ADDR'] ?
What do you all think? Suggestions? I work alone, so I don’t have a colleague to ask for help with this ... So my colleague, in some way.