473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Save ip address into DB

Hi there,

I'm saving ip addresses of blocked visitors into a mySQL DB.
The function with wich i retrieve the address is below this message.

What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.
But i don't know if it could mess up ip addresses. E.g. confuse
12.34.56.78 with 123.45.6.78

Frizzle.

--- function ---

function getIp()
{
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
return $_SERVER['REMOTE_ADDR'];
};
};

Feb 15 '06 #1
8 8443
frizzle wrote:
Hi there,

I'm saving ip addresses of blocked visitors into a mySQL DB.
The function with wich i retrieve the address is below this message.

What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.
But i don't know if it could mess up ip addresses. E.g. confuse
12.34.56.78 with 123.45.6.78

Frizzle.

--- function ---

function getIp()
{
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
return $_SERVER['REMOTE_ADDR'];
};
};


If you want it without periods, you need to make the fields line up:

$ip_address = '10.0.1.126';
$foo = explode('.', $ip_address);
$padded = sprintf("%03d%03d%03d%03d", $foo[0], $foo[1], $foo[2], $foo[3]);
printf("ip = %s, padded = %s\n", $ip_address, $padded);

However, if you are going to explode the ip address, why not just store
the octet elements individually?

Also, what about IPv6?

-david-

Feb 15 '06 #2
What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.


Use <http://www.php.net/manual/en/function.ip2long.php>.

---
Steve

Feb 15 '06 #3
frizzle wrote:
Hi there,

I'm saving ip addresses of blocked visitors into a mySQL DB.
The function with wich i retrieve the address is below this message.

What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.
But i don't know if it could mess up ip addresses. E.g. confuse
12.34.56.78 with 123.45.6.78

Frizzle.

--- function ---

function getIp()
{
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
return $_SERVER['REMOTE_ADDR'];
};
};


you cannot remove the dots from the IP unless you store it as

CLASSA (number), CLASSB (number),CLASSC (number),CLASSD (number)

123 45 6 78

But, the issue is that in order to do a comparison, you have to put them back
together - using SQL Query or PHP code - both very expensive.

I would leave it as is and store it intact. 123.45.6.78 it is much easier -
especially if the column in the db is indexed to do a string comparison.

select ipaddress from tablex where ipaddress='123.45.6.78'

select classa||'.'||classb||'.'||classc||'.'||classd
from tablex
where classa||'.'||classb||'.'||classc||'.'||classd='123 .45.6.78';

While they "look" like they are equivalent - the latter will cause a full table
scan to retrieve a single row. This probably would not be a problem until you
get several hundred,thousand records for each query.

Bottom line: leave it intact and index the column - I would try to use a unique
index so that you only store the address one time. (make sure you check to see
if it exists before trying to store it to prevent unique validation errors.

--
Michael Austin.
DBA Consultant.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Feb 16 '06 #4
Michael Austin wrote:
frizzle wrote:
Hi there,

I'm saving ip addresses of blocked visitors into a mySQL DB.
The function with wich i retrieve the address is below this message.

What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.
But i don't know if it could mess up ip addresses. E.g. confuse
12.34.56.78 with 123.45.6.78

Frizzle.

--- function ---

function getIp()
{
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
return $_SERVER['REMOTE_ADDR'];
};
};


you cannot remove the dots from the IP unless you store it as

CLASSA (number), CLASSB (number),CLASSC (number),CLASSD (number)

123 45 6 78

But, the issue is that in order to do a comparison, you have to put them back
together - using SQL Query or PHP code - both very expensive.

I would leave it as is and store it intact. 123.45.6.78 it is much easier -
especially if the column in the db is indexed to do a string comparison.

select ipaddress from tablex where ipaddress='123.45.6.78'

select classa||'.'||classb||'.'||classc||'.'||classd
from tablex
where classa||'.'||classb||'.'||classc||'.'||classd='123 .45.6.78';

While they "look" like they are equivalent - the latter will cause a full table
scan to retrieve a single row. This probably would not be a problem until you
get several hundred,thousand records for each query.

Bottom line: leave it intact and index the column - I would try to use a unique
index so that you only store the address one time. (make sure you check to see
if it exists before trying to store it to prevent unique validation errors.

--
Michael Austin.
DBA Consultant.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)


Thanks. So the advise is to save it in 1 field. If i'm right, i'd have
to be a varchar(15) field, unique, with an index on it?

Frizzle.

(PS, the function to get the IP is right though ? )

Feb 16 '06 #5
frizzle wrote:
Michael Austin wrote:
frizzle wrote:
Hi there,

I'm saving ip addresses of blocked visitors into a mySQL DB.
The function with wich i retrieve the address is below this message.

What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.
But i don't know if it could mess up ip addresses. E.g. confuse
12.34.56.78 with 123.45.6.78

Frizzle.

--- function ---

function getIp()
{
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
return $_SERVER['REMOTE_ADDR'];
};
};


you cannot remove the dots from the IP unless you store it as

CLASSA (number), CLASSB (number),CLASSC (number),CLASSD (number)

123 45 6 78

But, the issue is that in order to do a comparison, you have to put them back
together - using SQL Query or PHP code - both very expensive.

I would leave it as is and store it intact. 123.45.6.78 it is much easier -
especially if the column in the db is indexed to do a string comparison.

select ipaddress from tablex where ipaddress='123.45.6.78'

select classa||'.'||classb||'.'||classc||'.'||classd
from tablex
where classa||'.'||classb||'.'||classc||'.'||classd='123 .45.6.78';

While they "look" like they are equivalent - the latter will cause a full table
scan to retrieve a single row. This probably would not be a problem until you
get several hundred,thousand records for each query.

Bottom line: leave it intact and index the column - I would try to use a unique
index so that you only store the address one time. (make sure you check to see
if it exists before trying to store it to prevent unique validation errors.

--
Michael Austin.
DBA Consultant.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)

Thanks. So the advise is to save it in 1 field. If i'm right, i'd have
to be a varchar(15) field, unique, with an index on it?

Frizzle.

(PS, the function to get the IP is right though ? )


As Steve said, you can use ip2long to store it in your database as an
INT. Then use long2ip to get it back if necessary.

And searching on an INT will be much faster than searching on a VARCHAR(15).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 17 '06 #6
On 2006-02-15, frizzle <ph********@gmail.com> wrote:
Hi there,

I'm saving ip addresses of blocked visitors into a mySQL DB.
The function with wich i retrieve the address is below this message.

What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.
But i don't know if it could mess up ip addresses. E.g. confuse
12.34.56.78 with 123.45.6.78


if mysql has an IP address datatype like postgresql does use that.
--

Bye.
Jasen
Feb 17 '06 #7

Jasen Betts wrote:
On 2006-02-15, frizzle <ph********@gmail.com> wrote:
Hi there,

I'm saving ip addresses of blocked visitors into a mySQL DB.
The function with wich i retrieve the address is below this message.

What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.
But i don't know if it could mess up ip addresses. E.g. confuse
12.34.56.78 with 123.45.6.78


if mysql has an IP address datatype like postgresql does use that.
--

Bye.
Jasen


AFAIK it hasn't ...

Feb 19 '06 #8

"frizzle" <ph********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi there,

I'm saving ip addresses of blocked visitors into a mySQL DB.
The function with wich i retrieve the address is below this message.

What i wonder is, if it's ok to remove the dots from the ip address,
and only
save the numbers into the database. I assume it will be quicker to
compare.
But i don't know if it could mess up ip addresses. E.g. confuse
12.34.56.78 with 123.45.6.78

Frizzle.


try the database's built-in functions.
SELECT INET_ATON('209.207.224.40');
-> 3520061480

SELECT INET_NTOA(3520061480);
-> '209.207.224.40'
Mar 19 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
by: Steevo | last post by:
Any suggestions as to the best programs for cloaking email addresses? Many thanks -- Steevo
1
by: rob | last post by:
I built a combo box that contains names of people. A user can chooses a person from the combo box, the next couple of the text boxes will show the corresponding address of that person. I used...
2
by: Saxman | last post by:
I have one more problem to solve before completing my database. I need to save address labels to file. I know Label Wizard does this, but my problem needs to be selective. The customers are...
4
by: Jonny | last post by:
Hello Group How do I open a Save File Dialog from an ASPX page behind a browse button? Any help would be fantastic!! I am using ASP.NET 1.1 using VB.NET as the coding language TIA
10
by: rodrigo.gloria | last post by:
I am trying to convert a integer to an address of a function pointer. I want to encrypt the pointer and then do some validation, afterwards i will decrpyt the pointer back to an address. Can...
4
by: sufian | last post by:
Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message: echo("<div id=\"message\" class=\"success\">Thank you! You...
14
by: squrel | last post by:
Hello everyone, I m using some button using toolbar such as Add,Save,View,.... my save button is not working.... it doesnt give me any error but does not save to my database.... or showing in my...
5
by: terrybell105 | last post by:
I downloaded Stephan's utility from his website but can't get it to work - or maybe I'm not driving it properly! The form works OK with the existing 3 "views" - I can switch between them and they...
0
by: ling2000 | last post by:
Hello all, I'm trying to upgrade a VB6 project into VB.net, and the problem I had is in converting 'address of' to 'delegate'. I had the error "Value of type 'DelegateIDccManSink_OnLogIpAddr'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.