您在這裡

Drupal網站中顯示留言者的IP地址

James Qi 在 2016年11月22日 - 17:08 發表

  以前在Wiki網站中文章或者讨論的匿名發布者都是顯示出IP地址來(例如這個“周公解夢”頁面下方的用戶留言),這樣方便管理員或者其他用戶了解内容發布者的來源,也便于監控防範垃圾内容。而Drupal網站中留言的匿名發布者都是顯示“匿名”或者用戶自己留下的名稱和鍊接。

  今天嘗試了一下Drupal中修改模闆文件comment.tpl.php,是可以實現顯示留言者IP相關信息的,可以讓注冊用戶和匿名用戶的IP地址都顯示出來,具體代碼如下:

<?php

/**
 * @file
 * Bartik's theme implementation for comments.
 *
 * Available variables:
 * - $author: Comment author. Can be link or plain text.
 * - $content: An array of comment items. Use render($content) to print them all, or
 *   print a subset such as render($content['field_example']). Use
 *   hide($content['field_example']) to temporarily suppress the printing of a
 *   given element.
 * - $created: Formatted date and time for when the comment was created.
 *   Preprocess functions can reformat it by calling format_date() with the
 *   desired parameters on the $comment->created variable.
 * - $changed: Formatted date and time for when the comment was last changed.
 *   Preprocess functions can reformat it by calling format_date() with the
 *   desired parameters on the $comment->changed variable.
 * - $new: New comment marker.
 * - $permalink: Comment permalink.
 * - $submitted: Submission information created from $author and $created during
 *   template_preprocess_comment().
 * - $picture: Authors picture.
 * - $signature: Authors signature.
 * - $status: Comment status. Possible values are:
 *   comment-unpublished, comment-published or comment-preview.
 * - $title: Linked title.
 * - $classes: String of classes that can be used to style contextually through
 *   CSS. It can be manipulated through the variable $classes_array from
 *   preprocess functions. The default values can be one or more of the following:
 *   - comment: The current template type, i.e., "theming hook".
 *   - comment-by-anonymous: Comment by an unregistered user.
 *   - comment-by-node-author: Comment by the author of the parent node.
 *   - comment-preview: When previewing a new or edited comment.
 *   The following applies only to viewers who are registered users:
 *   - comment-unpublished: An unpublished comment visible only to administrators.
 *   - comment-by-viewer: Comment by the user currently viewing the page.
 *   - comment-new: New comment since last the visit.
 * - $title_prefix (array): An array containing additional output populated by
 *   modules, intended to be displayed in front of the main title tag that
 *   appears in the template.
 * - $title_suffix (array): An array containing additional output populated by
 *   modules, intended to be displayed after the main title tag that appears in
 *   the template.
 *
 * These two variables are provided for context:
 * - $comment: Full comment object.
 * - $node: Node object the comments are attached to.
 *
 * Other variables:
 * - $classes_array: Array of html class attribute values. It is flattened
 *   into a string within the variable $classes.
 *
 * @see template_preprocess()
 * @see template_preprocess_comment()
 * @see template_process()
 * @see theme_comment()
 */
?>
<article class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <footer class="attribution">

    <?php print $picture; ?>

    <div class="submitted">
      <p class="commenter-name">
        <?php print $author; ?>
      </p>
      <p class="comment-time">
        <?php print $created; ?>
      </p>
      <p class="comment-permalink">
        <?php print $permalink; ?>
      </p>
    </div>
  </footer>

  <div class="comment-text">
    <div class="comment-arrow"></div>

    <?php if ($new): ?>
      <span class="new"><?php print $new; ?></span>
    <?php endif; ?>

    <?php print render($title_prefix); ?>
    <h3<?php print $title_attributes; ?>><?php print $title; ?></h3>
    <?php print render($title_suffix); ?>

    <div class="content"<?php print $content_attributes; ?>>
<!-- 添加代碼開始 -->
<?php 
if (property_exists($comment,'hostname')) {
$output = '<p class="comment-time">';
$ip = $comment->hostname;
$location_link = l('位置',"http://zh-hans.ipshu.com/ipv4/$ip",array('attributes' => array('title' => "IP $ip 位置")));
$whois_link = l('誰是',"http://zh-hans.ipshu.com/whois_ipv4/$ip",array('attributes' => array('title' => "誰是 $ip 管理員")));
$output .= "-- 發自 IP地址: $ip ($location_link | $whois_link)";
$output .= "</p>";
print $output;
}
?>
<!-- 添加代碼結束 -->

<!-- Modify Code Start -->
<?php
if (property_exists($comment,'hostname')) {
$output = '<p class="comment-time">';
global $language;
$lang = $language->language;
$ip = $comment->hostname;
$location_link = l(t('Location'),"http://$lang.ipshu.com/ipv4/$ip",array('attributes' => array('title' => "IP $ip ".t('Location'))));
$whois_link = l(t('Whois'),"http://$lang.ipshu.com/whois_ipv4/$ip",array('attributes' => array('title' => t('Whois')." $ip ".t('Administrator'))));
$output .= "-- ".t('From')." IP ".t('Address').": $ip ($location_link | $whois_link)";
$output .= "</p>";
print $output;
}
?>
<!-- Modify Code End -->
      <?php
        // We hide the comments and links now so that we can render them later.
        hide($content['links']);
        print render($content);
      ?>
      <?php if ($signature): ?>
      <footer class="user-signature clearfix">
        <?php print $signature; ?>
      </footer>
      <?php endif; ?>
    </div> <!-- /.content -->

    <?php print render($content['links']); ?>
  </div> <!-- /.comment-text -->
</article>

  具體效果可以看本文下方的留言,IP地址的位置和whois信息指向了ipshu.com網站對應的頁面,點擊可以方便地查看IP地址來源等信息,查找、控制垃圾信息更方便。

  另外,如果希望node頁面顯示發布者IP就比comment留言顯示發布者IP麻煩一些,需要先在内容類型中添加一個專門記錄發布者IP的字段,在頁面保存的時候記錄下發布者IP,具體實現可以用Computed Code (PHP)字段,其默認值為
$entity_field[0]['value'] = ip_address();,更多辦法見:Get IP address of submitter 。

自由标簽:

回應

發表新回應

Plain text

  • 不允許使用 HTML 標籤。
  • 自動將網址與電子郵件地址轉變為連結。
  • 自動斷行和分段。