Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,907 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,769 people online right now. Registration is fast and FREE... Join Now!




HEX ip address

 
Reply to this topicStart new topic

HEX ip address

quick_nick
8 Mar, 2008 - 07:09 PM
Post #1

New D.I.C Head
*

Joined: 11 Sep, 2007
Posts: 10



Thanked: 1 times
My Contributions
I am trying to get an ip address then convert it into hex to be stored in a mysql database. So far i am having NO luck. This is the code i ran across on an Internet search.

CODE
  $ipi = getenv("REMOTE_ADDR");
function encode_ip($ipi) {
     // First thing to do, split the IP into its quads
     $ip = explode('.', $ipi);

     // Now we convert it and return it to the caller
     return sprintf('%02x%02x%02x%02x', $ip[0], $ip[1], $ip[2], $ip[3]);
}


I need the ip address to end up in a variable like
CODE
$IP_ADD = [the hex ip address]


Any help PLEASE

This post has been edited by quick_nick: 8 Mar, 2008 - 07:11 PM
User is offlineProfile CardPM
+Quote Post

quick_nick
RE: HEX Ip Address
8 Mar, 2008 - 07:17 PM
Post #2

New D.I.C Head
*

Joined: 11 Sep, 2007
Posts: 10



Thanked: 1 times
My Contributions
I got it figured out now
CODE

  $ipi = getenv("REMOTE_ADDR");
     $ip = explode('.', $ipi);
     $HEXIP = sprintf('%02x%02x%02x%02x', $ip[0], $ip[1], $ip[2], $ip[3]);

User is offlineProfile CardPM
+Quote Post

thehat
RE: HEX Ip Address
8 Mar, 2008 - 07:20 PM
Post #3

D.I.C Head
Group Icon

Joined: 28 Feb, 2008
Posts: 217


Dream Kudos: 100
My Contributions
You can convert a number to hex with dechex(). So, depending on how you want it to all end up:

php

$ipi = getenv("REMOTE_ADDR");
function encode_ip($ipi) {
// First thing to do, split the IP into its quads
$ip = explode('.', $ipi);

// Now we convert it and return it to the caller
for (i=0; i < count($ip); i++) {
$ip[i] = dechex($ip[i]);
}

//join the parts together
$IP_ADD = join(".",$ip);
}


Edit: But your way is neater smile.gif

This post has been edited by thehat: 8 Mar, 2008 - 07:56 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 09:52PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month