Saturday, February 27, 2010

IPv6 for online customers

Problem : how to make Magento show the ipv6 ip address on online customers.
See full text here http://www.magentocommerce.com/boards/viewthread/79160/

Possible (untested) solution. If someone tests it please tell me the results also:

In theory you cannot do this, because in the database the fields for ip address are 'bigint' (see tables that start with log_visitor_)
All the ip addresses stored in this table 'suffer' a modification with ip2long() method. Unfortunately php does not support IPv6 (at least that's what I know. If I'm wrong someone please correct me).
Enough with the problems.
Here is what you can try.
Change the type of the columns for ip addresses from bigint to varchar.
In app/code/core/Mage/Log/Model/Visitor.php look for these lines:
$this->addData(array(
'server_addr'           => $helper->getServerAddr(true),
'remote_addr'           => $helper->getRemoteAddr(true), 
....
and remove the 'true' parameters from the functions.
$this->addData(array(
'server_addr'           => $helper->getServerAddr(),
'remote_addr'           => $helper->getRemoteAddr(), 
This should save the ip addresses as strings.

We are half way there. Now all you have to do is make sure that the ip addresses are rendered in the back-end grid you need to change the \app\code\core\Mage\Adminhtml\Block\Customer\Online\Grid\Renderer\Ip.php file.
Change this
public function render(Varien_Object $row)
{
return long2ip($row->getData($this->getColumn()->getIndex()));
}

to
public function render(Varien_Object $row)
{
return $row->getData($this->getColumn()->getIndex());
}

or to this, for backwards compatibility (to see the previous saved ip addresses)
public function render(Varien_Object $row)
{
$val = $this->getColumn()->getIndex();
if (is_int($val)){
return long2ip($val);
}
return $val;
}

Clear the cache and test it.
I have no way of testing this but it seams to me that this is the way to go.

(Of course you don't need to modify the core files if you plan to upgrade, you can overwrite them the proper magento way ).

4 comments:

  1. Hi, thanks for your're post, I'm Sorry to inform you, it dosn't work with Magento 1.9.1.0 :(...
    I become wrong numbers without points or "RemoteAddr" as output.

    ReplyDelete
  2. If I chance to

    public function render(Varien_Object $row)
    {
    return Mage::helper('core/http')->getRemoteAddr();
    }

    }


    I See IPv6, but is the wrong :(.

    Have you a idea, whats wrong?

    ReplyDelete
    Replies
    1. Sorry. I have no idea. i didn't check this in a long time.

      Delete