0

My site is on drupal 6.19, but it always shows some "page not found" to anonymous users. Now I want to write code to examine the cache_page table to know which pages are cached as "page not found", and remove such records from the table.

Which code should I write, and where should I put such code to make it work?

1
  • Um, I have no idea what you're trying to achieve here. This makes no sense to me. What do you want to do, in terms of desired functionality?
    – Greg
    Mar 23 '11 at 8:12
2

As already reported from marcvangend, if Drupal is returning a "page not found" error when it should not be, then it is probable Drupal (or the server running it) is not correctly set. It should not be a permission problem, as in such cases Drupal would return an "access denied" error.

The code to delete records in the cache_page table for pages that returned the "page not found" error is the following:

db_query("DELETE FROM {cache_table} WHERE headers LIKE '%404 Not Found'");

You can create a custom module (e.g. mymodule.module) that contains the following code:

/**
 * Implements hook_footer().
 */
function mymodule_footer($main = 0) {
  db_query("DELETE FROM {cache_table} WHERE headers LIKE '%404 Not Found'");
}

I used hook_footer() because it is always invoked all times Drupal displays a page; the hook could also not return any HTML, as shown in the example reported in the documentation page for hook_footer().

function hook_footer($main = 0) {
  if (variable_get('dev_query', 0)) {
    return '<div style="clear:both;">' . devel_query_table() . '</div>';
  }
}

hook_footer() is not used by Drupal 7, and it is probably not executed for cached pages. For compatibility reasons (which means that few of the module code would be changed when passing to Drupal 7), I would implement hook_exit(), which is used also in Drupal 7.

/**
 * Implements hook_exit().
 */
function mymodule_exit() {
  db_query("DELETE FROM {cache_table} WHERE headers LIKE '%404 Not Found'");
}

The only "problem" would be with modules that call exit() without to first call module_invoke_all('exit'); in that case the implementations of hook_exit() are not invoked, but it should be a minor problem, in this specific case.

As alternative, you could implement hook_cron(). Keep in mind that hook_cron() is invoked periodically, every day or every hour, depending from the settings of the cron task for the server where your website runs on.
If you have Poormanscron installed, then you can set every hook_cron() implementations to run every hour. As the module uses JavaScript code to run the implementations of hook_cron(), they are really executed when somebody visits your site.

/**
 * Implements hook_cron().
 */
function mymodule_cron() {
  db_query("DELETE FROM {cache_table} WHERE headers LIKE '%404 Not Found'");
}
4
  • why your useing hook_footer().i referenced the api. it says Insert closing HTML.
    – enjoylife
    Mar 23 '11 at 13:18
  • I am using hook_footer() because it is used every time a page is served from Drupal. I could have used hook_cron(), but that is called only during the cron tasks.
    – apaderno
    Mar 23 '11 at 13:28
  • I expanded my answer to explain why I used that hook, and which hook I would alternatively use to execute the code I am suggesting.
    – apaderno
    Mar 23 '11 at 13:44
  • Isn't it bad practice to use hook_footer for this when you can use hook_exit? Hook footer was specifically designed to insert markup into a page.
    – Alex Weber
    Mar 23 '11 at 14:39
1

Sounds like you're fixing the wrong problem. If url's that should return valid pages, return 404's instead, I would start with looking at the way my menu is cached and if something strange is happening with permissions and access callbacks.

2
  • drupal.stackexchange.com/questions/1038/… this is my site's problem. i don't know how to fix it and examin it
    – enjoylife
    Mar 23 '11 at 8:30
  • That looks like a pretty good answer to me. It contains some good starting points, and a question to you that you didn't answer yet. I'd say: stick to that answer, answer Drew, and take it from there. IMHO this question can be closed. Mar 23 '11 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.