views:

62

answers:

4

I am on a project using php and codeigniter framework. I want to make changes in the database every 3 months. The problem is I don't know how to do it with PHP. I can activate a function when a user access a page, but I want it to be self-executed at certain date.

+1  A: 

You need to setup a cron job (unix) or a scheduled task (windows) that will do this for you.

It is very bad practice to rely on a user hiting a page to do scheduled tasks.

Byron Whitlock
+4  A: 

You can code your script in PHP, and run it with CRON (provided you are hosting your application in a UNIX/LINUX environment).

Here is a WebSite explaining how to do your automated PHP execution with CRON.

Pablo Santa Cruz
+1 good link, but he will have to write a command line specific bootstrap for Codeigniter.
Byron Whitlock
A: 

If you don't have access to cron, you can simply plant a code at your main page that checks the date upon every visitor, and do the necessary tasks if the current date conforms with the modulo condition.

liorda
A: 

Setup a cronjob and make your own bootstrap file (you'll call this file and pass the class and method as command line params.

This is a quick and dirty way of doing it (totally untested)

// usage: php cmd.php users/cron
if (php_sapi_name() != 'cli' || count($argv) != 2) {
    exit('Not enough params');
}

// Fake REQUEST_URI
$_SERVER['REQUEST_URI']  =  $argv[1];

// include the index bootstrap file
require 'index.php';

Name the file cmd.php and setup a cron job to run every month.

usage:

cd root/to/directory && php cmd.php users/prune

This is just an un-tested example - but should point you in the right direction :)

Kieran Allen