Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like anonymous users to be able to comment only once on a node. I thought about checking whether there's already a comment from the user's IP-adress and, if yes, hiding the comment form, but I don't know how to do that.

How can I achieve this?

share|improve this question
up vote 5 down vote accepted

You can do this by means of two functions called flood_register_event() and flood_is_allowed().

When a anon user views a node, call flood_is_allowed in hook_node_view that sets COMMENT_NODE_CLOSED for the node.

When a anon user comments, register that as a flood event in hook_comment_presave for that particular node. By setting the $threshold (parameter for flood_is_allowed) to 1, the user will be stopped from commenting after the first comment is posted.

This is the bare bones of how to implement hooks that make use of these functions:

function MYMODULE_comment_presave($comment) {
  global $user;
  if (!$user->uid) {
function MYMODULE_node_view($node, $view_mode, $langcode) {
  global $user;
  if (!$user->uid && !flood_is_allowed($node->nid, 1)) {
    // This bars the anon user from posting a second comment of the node.
    // For a more sophisticated blocking method (e.g. to prevent the comment form from
    // showing up), ask another question about how to do that.
    $node->comment = COMMENT_NODE_CLOSED;

PS: The module Anonymous Publishing is already using this pair of functions to limit the number of comments an anonymous user can post (but not exactly in the way you specify). However, looking at the source code of that module should give you an idea about how to use this pair of functions for flood control of anonymous comments.

share|improve this answer
Thanks for the code! I've added it to my custom module, but it doesn't seem to work. Is that normal? It looks fine to me. Did you test it? – Jeroen Apr 13 '14 at 21:35
Yes, I've tested it. As I said; It is bare bones, to show how to use these two functions to prevent the anon user from posting more than one comment for a single node within one hour. There is plenty of work (such as actually removing the comment form from display, or extending the period the flood control is enforced) left for you to do. – Free Radical Apr 14 '14 at 2:16
Thanks again for your help. I've been able to let it work. Your code wasn't very far from the solution. I found the last necessary hint at drupal.stackexchange.com/questions/4513/…. – Jeroen Apr 14 '14 at 20:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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