Addon: IP Lookup link on access and error logs

Discussion in 'Customization & add-ons' started by amadeus, Jun 30, 2009.

  1. amadeus

    amadeus aMember Pro Customer

    Joined:
    Jan 16, 2009
    Messages:
    42
    I got tired of cutting/pasting IP addresses from my access/error logs to www.whatismyip.com, so, I decided to add a little link for each record :)

    Enjoy!


    In /templates/admin/access_log.html:
    ------------------

    Replace:

    Code:
    {foreach from=$list item=p}
    <tr class={cycle values="xx,odd"}>
        <td> {$p.time|date_format:$config.time_format} </td>
        <td> <a href="users.php?action=edit&member_id={$p.member_id}">{$p.login}</a> </td>
        <td> {$p.url|escape} </td>
    	{if !$config.demo}
        <td> {$p.remote_addr|escape} </td>
    	{/if}
        <td> {$p.referrer|escape} </td>
    </tr>
    {/foreach}
    With:
    Code:
    {foreach from=$list item=p name=loop}
    <tr class={cycle values="xx,odd"}>
        <td> {$p.time|date_format:$config.time_format} </td>
        <td> <a href="users.php?action=edit&member_id={$p.member_id}">{$p.login}</a> </td>
        <td> {$p.url|escape} </td>
    	{if !$config.demo}
        <td> {$p.remote_addr|escape}
              &nbsp;&nbsp;&nbsp;<span style="font-size: 9px;"><a href="#" onclick="document.ip_lookup_form{$smarty.foreach.loop.iteration}.submit()">Lookup</a></span>
              &nbsp;&nbsp;&nbsp;<span style="font-size: 9px;"><a href="#" onclick="document.whois_lookup_form{$smarty.foreach.loop.iteration}.submit()">WHOIS</a></span>
              <FORM METHOD="POST" ACTION="http://www.whatismyip.com/tools/ip-address-lookup.asp" TARGET="_blank" NAME="ip_lookup_form{$smarty.foreach.loop.iteration}">
                  <input type="hidden" name="IP" value="{$p.remote_addr|escape}" />              
                  <input type="hidden" value="Lookup" name="GL" />
              </FORM>
              
              <FORM METHOD="POST" ACTION="http://www.whatismyip.com/tools/ip-whois-lookup.asp" TARGET="_blank" NAME="whois_lookup_form{$smarty.foreach.loop.iteration}">
                   <input type="hidden" name="IP" value="{$p.remote_addr|escape}" />              
                   <input type="hidden" value="Whois Lookup" name="WL" />
              </FORM>          
        </td>
    	{/if}
        <td> {$p.referrer|escape} </td>
    </tr>
    {/foreach}

    In /templates/admin/error_log.html:
    ------------------

    Replace:
    Code:
    {foreach from=$list item=p}
    <tr>
        <td> {$p.time|date_format:$config.time_format} </td>
        <td> {$p.url|truncate:60|escape} </td>
    	{if !$config.demo}
        <td> {$p.remote_addr|escape} </td>
    	{/if}
        <td> <SMALL>{$p.error|escape|replace:"&lt;br&gt;":"<br>"|replace:"&lt;br /&gt;":"<br />"}</SMALL> </td>
    </tr>
    {/foreach}
    With:

    Code:
    {foreach from=$list item=p name=loop}
    <tr>
        <td> {$p.time|date_format:$config.time_format} </td>
        <td> {$p.url|truncate:60|escape} </td>
    	{if !$config.demo}
        <td> {$p.remote_addr|escape}
              &nbsp;&nbsp;&nbsp;<span style="font-size: 9px;"><a href="#" onclick="document.ip_lookup_form{$smarty.foreach.loop.iteration}.submit()">Lookup</a></span>
              &nbsp;&nbsp;&nbsp;<span style="font-size: 9px;"><a href="#" onclick="document.whois_lookup_form{$smarty.foreach.loop.iteration}.submit()">WHOIS</a></span>
              <FORM METHOD="POST" ACTION="http://www.whatismyip.com/tools/ip-address-lookup.asp" TARGET="_blank" NAME="ip_lookup_form{$smarty.foreach.loop.iteration}">
                     <input type="hidden" name="IP" value="{$p.remote_addr|escape}" />              
                     <input type="hidden" value="Lookup" name="GL" />
              </FORM>
              
              <FORM METHOD="POST" ACTION="http://www.whatismyip.com/tools/ip-whois-lookup.asp" TARGET="_blank" NAME="whois_lookup_form{$smarty.foreach.loop.iteration}">
                     <input type="hidden" name="IP" value="{$p.remote_addr|escape}" />              
                     <input type="hidden" value="Whois Lookup" name="WL" />
              </FORM>          
        </td>
    	{/if}
        <td> <SMALL>{$p.error|escape|replace:"&lt;br&gt;":"<br>"|replace:"&lt;br /&gt;":"<br />"}</SMALL> </td>
    </tr>
    {/foreach}
    In /templates/admin/user_access_log.html:
    ------------------

    Replace:

    Code:
    {foreach from=$list item=p}
    <tr class={cycle values="xx,odd"}>
        <td> {$p.time|date_format:$config.time_format} </td>
        <td> {$p.login} </td>
        <td> {$p.url|escape} </td>
        <td> {$p.remote_addr|escape} </td>
        <td> {$p.referrer|escape} </td>
    </tr>
    {/foreach}
    With:

    Code:
    {foreach from=$list item=p name=loop}
    <tr class={cycle values="xx,odd"}>
        <td> {$p.time|date_format:$config.time_format} </td>
        <td> {$p.login} </td>
        <td> {$p.url|escape} </td>
        <td> {$p.remote_addr|escape}
              &nbsp;&nbsp;&nbsp;<span style="font-size: 9px;"><a href="#" onclick="document.ip_lookup_form{$smarty.foreach.loop.iteration}.submit()">Lookup</a></span>
              &nbsp;&nbsp;&nbsp;<span style="font-size: 9px;"><a href="#" onclick="document.whois_lookup_form{$smarty.foreach.loop.iteration}.submit()">WHOIS</a></span>
             <FORM METHOD="POST" ACTION="http://www.whatismyip.com/tools/ip-address-lookup.asp" TARGET="_blank" NAME="ip_lookup_form{$smarty.foreach.loop.iteration}">
                   <input type="hidden" name="IP" value="{$p.remote_addr|escape}" />              
                   <input type="hidden" value="Lookup" name="GL" />
            </FORM>
              
             <FORM METHOD="POST" ACTION="http://www.whatismyip.com/tools/ip-whois-lookup.asp" TARGET="_blank" NAME="whois_lookup_form{$smarty.foreach.loop.iteration}">
                   <input type="hidden" name="IP" value="{$p.remote_addr|escape}" />              
                   <input type="hidden" value="Whois Lookup" name="WL" />
             </FORM>          
        </td>
        <td> {$p.referrer|escape} </td>
    </tr>
    {/foreach}
  2. amadeus

    amadeus aMember Pro Customer

    Joined:
    Jan 16, 2009
    Messages:
    42
    Ok -- I just updated this posting with the following modifications:

    * Added "WHOIS" link in addition to the "Lookup" link
    * Made the links MUCH smaller (helps sooo much with formatting!)
    * Added the links to user_access_log.html as well
  3. skippybosco

    skippybosco CGI-Central Partner Staff Member

    Joined:
    Aug 22, 2006
    Messages:
    2,526
    @amadeus: Thanks so much for sharing. This is a great option!
  4. deafdavid

    deafdavid Member

    Joined:
    Aug 28, 2006
    Messages:
    153
    Pardon my ignorance..what's the purpose of having this IP Lookup?
  5. skippybosco

    skippybosco CGI-Central Partner Staff Member

    Joined:
    Aug 22, 2006
    Messages:
    2,526
    An example would be when you see suspicious activity in your access logs and you want to research information about the IP address (to match up with customer location, etc)
  6. deafdavid

    deafdavid Member

    Joined:
    Aug 28, 2006
    Messages:
    153
    Skip, I thought so. But IP address is often changed -- maybe one day it'll be in Germany, next day, it may be in Italy. No IP addresses stay in same country or location for long (I don't recall how long IP addresses usually are assigned to one location -- I think few days). So looking up IP next day may not be much of help. Am I wrong? (P.S. Thanks for taking the time to educate me (and others)).
  7. miso

    miso aMember Pro Customer

    Joined:
    Aug 22, 2006
    Messages:
    542
    Well, that's exactly why it's helpful - if you see that certain account's IP is bouncing all over the world, that will tell you right there that it might have been compromised or something ;)
  8. mageadmin

    mageadmin New Member

    Joined:
    Jun 8, 2009
    Messages:
    4
    this is very useful. Is there a way it could be altered to just display the country of the IP right there in the amember dashboard?
  9. ptrmee

    ptrmee aMember Pro Customer

    Joined:
    Jul 29, 2009
    Messages:
    10
    not quite the country but full ISP details. This is a partial edit of amember/templates/admin/user_access_log.php

    Code:
    <table class=hedit width=70%>
    <tr>
        <th>Time</th>
        <th>Member</th>
        <th>URL</th>
        <th>Remote IP</th>
        <th>ISP</th> <!-- NEW-->
        <th>HTTP Referrer</th>
    </tr>
    {foreach from=$list item=p}
    
    <!-- This entire php section is  NEW. It extracts the p array from smarty and looks up the ISP details -->
    
    {php} 
    $p =  $this->get_template_vars('p');
    $ip = $p['remote_addr'];
    $fullhost = gethostbyaddr($ip);
    $host = preg_replace("/^[^.]+./", "", $fullhost);
    {/php}
    
    <!-- end new PHP section -->
    
    <tr class={cycle values="xx,odd"}>
        <td> {$p.time|date_format:$config.time_format} </td>
        <td> {$p.login} </td>
        <td> {$p.url|escape} </td>
        <td> {$p.remote_addr|escape} </td>
         <td> <?php  echo $fullhost ?> </td> <!-- NEW -->
        <td> {$p.referrer|escape} </td>
    </tr>
    {/foreach}
    Peter
  10. amadeus

    amadeus aMember Pro Customer

    Joined:
    Jan 16, 2009
    Messages:
    42
    I just wanted to be clear that I relinquish all copyrights on my code in this thread (in hopes that Alex will add it, or something similar, to the standard aMember distribution.) :)

Share This Page