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?

1 Answer 1


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.

  • 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, 2014 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. Apr 14, 2014 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, 2014 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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