942,959 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 580
  • PHP RSS
Jul 29th, 2010
0

Storing IP Address

Expand Post »
Alright, the register/login system is fully working with sql injection, BUUUT now I want to store their IP Address. WHOLE new function that I've never dealt with before. So I need some help. Am I doing something wrong?

I have the IPadd in the database as INT(10) and then I went down to another drop down list and hit unsigned.
I feel like I'm doing something wrong with the field in the database..

PHP Syntax (Toggle Plain Text)
  1. {
  2. $_SERVER['REMOTE_ADDR'] = $IPadd;
  3. $sql = "INSERT INTO Member (username,createDate,password,firstName,email,IPadd) VALUES ('$username',NOW(),md5('$password'),'$firstName','$email',inet_aton('$IPadd'))";
  4. mysqli_query($cxn,$sql);
  5. $_SESSION['auth']="yes";
  6. $_SESSION['username'] = $username;
  7. header("Location: testing.php");
  8. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
Tenaciousmug is offline Offline
46 posts
since Jun 2010
Jul 29th, 2010
2
Re: Storing IP Address
Hello,

This is what I used for reference and it says INT UNSIGNED (4 bytes) which is how mine are defined and they work great.

MySQL has two built-in functions: INET_ATON() and INET_NTOA(). They are actually based on the equivalent inet_aton() and inet_ntoa() which are C library functions present on pretty much every TCP/IP capable system. Why? These two functions are used allover the place in any TCP/IP stack implementation or even application.
The INET_ATON() function converts Internet addresses from the numbers-and-dots notation into a 32-bit unsigned integer, and INET_NTOA() does the opposite. Isn't that handy!

Let's put it to the test:

mysql> SELECT INET_ATON('192.168.0.10') AS ipn;
+------------+
| ipn |
+------------+
| 3232235530 |
+------------+

mysql> SELECT INET_NTOA(3232235530) AS ipa;
+--------------+
| ipa |
+--------------+
| 192.168.0.10 |
+--------------+

So you can store an IP address in an INT UNSIGNED (4 bytes) which is of course much more efficient and faster than a CHAR(15). Naturally, you can call the function while you're inserting, so something like this is fine also:

INSERT INTO tbl VALUES (..., INET_ATON('192.168.0.10'), ...)
Reputation Points: 101
Solved Threads: 134
Practically a Posting Shark
rch1231 is offline Offline
870 posts
since Sep 2009
Jul 29th, 2010
0
Re: Storing IP Address
...
I said I am using INT(10) UNSIGNED and I have the coding right.
I have exactly what they have.

I MUST still being doing something wrong though because it's not working. It fails to insert it into the table..

I dont know peoples IP address so I can't do INET_ATON('numbers here'), but I pull their IP address with the REMOTE_ADDR function and make it into a variable and put that into the parantheses but it's STILL not working.
It's failing to insert so I must be doing something wrong with the actual IPadd field in the Mysql database..

http://i370.photobucket.com/albums/o...emberTable.png

Also random question, is it ok if I make a database called Asterock_memberdirectory and just use that database for everything like member, login, news, pet, items?

EDIT
NEVERMIND. Someone helped me on phpfreaks. All I had to do was switch the function and variable around. xD
Last edited by Tenaciousmug; Jul 29th, 2010 at 8:35 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
Tenaciousmug is offline Offline
46 posts
since Jun 2010
Jul 30th, 2010
0
Re: Storing IP Address
An IP address of the most commonly used IPV4 protocol is basically a 32-bit number. The INT data type would have the highest storage density for it. Depending of the form in which you have the address, you might need to convert it into na integer though. The disadvantage is that the number is inconvenient for humans when viewing the table contents with standard tools.


You could indeed store the IP addres in an INT UNSIGNED:

mysql> SELECT INET_ATON('192.168.0.10') AS ipn;
+------------+
| ipn |
+------------+
| 3232235530 |
+------------+

mysql> SELECT INET_NTOA(3232235530) AS ipa;
+--------------+
| ipa |
+--------------+
| 192.168.0.10 |
+--------------+

So you can store an IP address in an INT UNSIGNED (4 bytes) which is of course much more efficient and faster than a CHAR(15). Naturally, you can call the function while you're inserting, so something like this is fine also:

INSERT INTO tbl VALUES (..., INET_ATON('192.168.0.10'), ...)
Reputation Points: 21
Solved Threads: 35
Posting Pro
muralikalpana is offline Offline
534 posts
since Sep 2009
Jul 30th, 2010
0
Re: Storing IP Address
what is it? where you giving this value? (IPadd)
PHP Syntax (Toggle Plain Text)
  1. $_SERVER['REMOTE_ADDR'] = $IPadd;
maybe you need this...
PHP Syntax (Toggle Plain Text)
  1. $IPadd = $_SERVER['REMOTE_ADDR'];
Reputation Points: 10
Solved Threads: 3
Newbie Poster
ZZZubec is offline Offline
19 posts
since Sep 2009
Jul 30th, 2010
0
Re: Storing IP Address
Not sure but i think this code useful to your query
IP address using HTTP_X_FORWARDED_FOR. And if this is also not available, then finally get the IP address using REMOTE_ADDR.

function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
Reputation Points: 11
Solved Threads: 1
Light Poster
manzarr is offline Offline
39 posts
since Jul 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Style text in a text area...
Next Thread in PHP Forum Timeline: Create a link for a file stored





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC