Drupal: show IP address of comment author
stplanken — September 23, 2009 - 12:09

There is no standard way to display the IP address (hostname) of the author of a comment. Since I urgently needed this for auditing purposes (not this site), I created a simple template function.
In template.php, I use this:
<?php
function phptemplate_comment_submitted($comment) {
//Text to filter from user name
$notverified = ' (not verified)';
//Return entire string
return t('Submitted: !datetime — !username',
array(
'!username' => str_replace($notverified, '', theme('username', $comment)),
'!datetime' => format_date($comment->timestamp)
)
);
}
?>In comment.tpl.php I then use this to display the posted by info:
<?php if ($submitted) { ?>
<div class="submitted">
<?php print $submitted; ?>
</div>
<?php } ?>The combination of both returns the result:
Submitted: September 22, 2009 - 10:29 — splanken.
In order to add the hostname of the comment author, I added a database query to retrieve the hostname from the comments table:
<?php
function phptemplate_comment_submitted($comment) {
//Retrieve IP address of comment author
$hostname = '';
$result = db_query("
SELECT c.hostname
FROM {comments} c
WHERE c.cid=%d
LIMIT 1
", $comment->cid);
while ($item = db_fetch_object($result)) {
$hostname .= $item->hostname;
}
//Text to filter from user name
$notverified = ' (not verified)';
//Return entire string
return t('Submitted: !datetime — !username (!hostname)',
array(
'!username' => str_replace($notverified, '', theme('username', $comment)),
'!datetime' => format_date($comment->timestamp),
'!hostname' => $hostname
)
);
}
?>Which then displays:
Submitted: September 22, 2009 - 10:29 — splanken (10.6.23.158)
