473,238 Members | 1,747 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,238 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 8440
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.