แสดง ip ของคนที่แสดงความคิดเห็นใน drupal

พอดีผมไปอ่านเจอในบอร์ดของ drupal.in.th ว่ามีคนต้องการให้แสดง ip address ของคนที่มาโพสความคิดเห็นด้วย ผมเลยลองไปหามาเล่นๆดูว่าจะทำอย่างไง และผมก็ได้วิธีทำมาครับซึ่งไม่ยากเท่าไร

โดยปกติแล้วใน omment.tpl.php คำสั่งที่ใช้แสดงผลของวันที่และผู้ใช้งานจะเป็น

<?php if ($submitted) { ?>
  <div class="submitted">
    <?php print $submitted; ?>
  </div>
<?php } ?>

ซึ่งก็จะได้ผลตามนี้

Submitted: September 22, 2010 - 10:29 - snappy

ถ้าเราอยากให้แสดง ip address ด้วยก็ต้องไปเพิ่มใน template.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
    )
  );
}

และผลที่ได้ก็จะออกมาเป็น

Submitted: September 22, 2010 - 10:29 - snappy (10.6.23.158)

thank : http://planken.org/2009/09/23/drupal-show-ip-address-comment-author

บทความแนะนำ
blog comments powered by Disqus