Running cron as administrator or another user in Drupal
Here are some links and notes about cron on Drupal 6. When you let the server run cron on your Drupal installation it runs as Anonymous user (uid=0) and if you have some tasks that requires admin access it will leave those. One workaround is to temporary fake another user, like it was admin (uid=1) who run the cronjob.
This is a safer way to temporary change the user because if the function crashes it has not saved the temporary user. If the function succeed it returns to Anonymous user when finished.
<?php
global $user;
$original_user = $user;
session_save_session(FALSE); // D7: use drupal_save_session(FALSE);
$user = user_load(array('uid' => 1)); // D7: use user_load(1);
// Take your action here where you pretend to be the user with UID = 1 (typically the admin user on a site)
// If your code fails, it's not a problem because the session will not be saved
$user = $original_user;
session_save_session(TRUE); // // D7: use drupal_save_session(TRUE);
// From here on the $user is back to normal so it's OK for the session to be saved
?>
Knowledge keywords: