Action: GetIP

Current version:
Credits:

{{getip}}


/action/getip.php


<?php
    
// determine IP
    
$tpl->ip $ip $this->get_user_ip();
    
    
// resolve IP and determine host
    
$tpl->host gethostbyaddr($ip);


/action/template/getip.tpl

[ === main === ]
 
[ ' ip ' ]<br>
[ ' host ' ]<br>

Comments

  1. 评论 4240

     templatest: no main template found in action/template/getip.tpl


    Add failed.
    I don't understand templates.
    If I can succeed, I want to modify the code to display part of the IP and publish the IP of the page, not that of the visitor.

     
    
    $dot = strripos($ip,"."); //查找“.”最后出现的位置
    $hide_ip = substr($ip,0,$dot).".*"; //输出“.”最后出现位置之前的字符串并加上*号
    echo $hide_ip;


    If the IP is 111.111.111.111, display 111.111.111.*,or 111.111.*.*
  2. This was added only as a snippet

    I added the main definition declaration to the example above, so it will work out of the box.

    You probably want wrap it in $this->get_user() condition so it won't be cached.
    
    <?php

    if ($this->get_user())
    {
        
    $dot        strripos($ip'.');
        
    $tpl->ip    substr($ip0$dot) . '.*';
    }

    Or disable the page cache for guests, which is a rather less good option, it is used primarily for Captcha and form tokens.
    
    <?php

    // disable server cache for page
    $this->http->no_cache(false);


    The get_user_ip() function itself already contains a simple anonymize feature if the config setting anonymize_ip is enabled.
    
    <?php

    function get_user_ip()
    {
        if (
    $this->db->anonymize_ip)
        {
            return 
    '0.0.0.0';
        }
        else
        {
            return 
    $this->http->ip;
        }
    }
    • WikiAdmin
    • 22.05.2022 07:08 edited