How do I redirect back to the source page after the user submits a comment? How can I access the nid of the current page so that a user can submit their comment and be returned back to the page they just commented from. Here's what I am proposing...

function mymodule_form_comment_form_alter(&amp;$form, &amp;$form_state) {
  $nid = arg(2);
  $form_state['redirect'] = 'mymodule/' . $nid;
}


...to be placed in template.php. However using $node -&gt; nid (referenced in comment.module) or $nid (referenced in my custom module) throws an undefined error. How can I access the nid to complete my redirecting URL? Or is there a better way of acheiving this?

I believe this is the part of comment.module that I need to hook into and modify - so in a wider sense my question is about hooking into a core module and modifying its function:

/**
 * Process comment form submissions; prepare the comment, store it, and set a redirection target.
 */
function comment_form_submit($form, &amp;$form_state) {
  $node = node_load($form_state['values']['nid']);
  $comment = comment_form_submit_build_comment($form, $form_state);
  if (user_access('post comments') &amp;&amp; (user_access('administer comments') || $node-&gt;comment == COMMENT_NODE_OPEN)) {
    // Save the anonymous user information to a cookie for reuse.
    if (user_is_anonymous()) {
      user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage'))));
    }

    comment_save($comment);
    $form_state['values']['cid'] = $comment-&gt;cid;

    // Add an entry to the watchdog log.
    watchdog('content', 'Comment posted: %subject.', array('%subject' =&gt; $comment-&gt;subject), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment-&gt;cid, array('fragment' =&gt; 'comment-' . $comment-&gt;cid)));

    // Explain the approval queue if necessary.
    if ($comment-&gt;status == COMMENT_NOT_PUBLISHED) {
      if (!user_access('administer comments')) {
        drupal_set_message(t('Your comment has been queued for review by site administrators and will be published after approval.'));
      }
    }
    else {
      drupal_set_message(t('Your comment has been posted.'));
    }
    $query = array();
    // Find the current display page for this comment.
    $page = comment_get_display_page($comment-&gt;cid, $node-&gt;type);
    if ($page &gt; 0) {
      $query['page'] = $page;
    }
    // Redirect to the newly posted comment.
    $redirect = array('node/' . $node-&gt;nid, array('query' =&gt; $query, 'fragment' =&gt; 'comment-' . $comment-&gt;cid));
  }
  else {
    watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' =&gt; $comment-&gt;subject), WATCHDOG_WARNING);
    drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' =&gt; $comment-&gt;subject)), 'error');
    // Redirect the user to the node they are commenting on.
    $redirect = 'node/' . $node-&gt;nid;
  }
  $form_state['redirect'] = $redirect;
  // Clear the block and page caches so that anonymous users see the comment
  // they have posted.
  cache_clear_all();
}


Thanks.
To redirect a form after submission you would do this:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_comment_form_alter(&amp;$form, &amp;$form_state) {
  // Add a custom submit handler to act when the form submits.
  $form['#submit'][] = 'MYMODULE_custom_comment_submit_handler';
}

/**
 * Custom submit handler for the comment form.
 */
function MYMODULE_custom_comment_submit_handler($form, &amp;$form_state) {
  // Redirect the user after submission
  $nid = arg(2);
  $form_state['redirect'] = 'mymodule/' . $nid;
}