Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IP address storing in MySQL database using PHP [duplicate]

what is the right field type for IP address in mysql? and what is the right way of storing it using PHP?

like image 227
user552828 Avatar asked Jun 21 '11 15:06

user552828


People also ask

How can I get IP address and store in database using PHP?

You can try this one also. $ip=$_SERVER['REMOTE_ADDR']; echo "IP address= $ip"; If your application hosted on same machine from where you are trying to request it will always return '::1', It means LocalHost. else it will return client IP Address.

What is data type for IP address in MySQL?

MySQL. While MySQL doesn't have a data type for IP addresses, it's still easy to compare IPs in MySQL. Using inet_aton you can convert your IP addresses to integers before you compare.

How do I find the IP address of a MySQL database?

GLOBAL_VARIABLES where VARIABLE_NAME like 'hostname'; SELECT host FROM information_schema. processlist WHERE ID=connection_id(); Will give you the host name (or IP address if name resolution is not enabled, which it is usually not) connecting to the mysql server on the current connection.

How can I get static IP in PHP?

The simplest way is to use $_SERVER with 'REMOTE_ADDR', it will return the user's IP address who is currently viewing the page. Example using ['REMOTE_ADDR'] to identify the server's IP address in PHP.

How to store IP address in MySQL database?

An IP address is typically written in decimal digits, formatted as four 8-bit fields separated by periods. Each 8-bit field represents a byte of the IP address. To store IP address in MySQL, use integer data type. Using INT data type we can save more space in database. As you can see we have taken int (4) unsigned to store IP address.

How to insert visitor logs in the database using PHP and MySQL?

Insert visitor logs in the database using PHP and MySQL. The prepared statement (prepare, bind and execute) is used to insert log data into the MySQL database. Include the Log script ( log.php) in the web page to store visitor logs in the database.

How to connect the database using PHP and MySQL?

The dbConfig.php file is used to connect the database using PHP and MySQL. Specify the database host ( $dbHost ), username ( $dbUsername ), password ( $dbPassword ), and name ( $dbName) as per your database credentials. Most of the server and browser related informations are retrieved with $_SERVER, a super global variable in PHP.

How to access the IP address into it's format?

To access the IP address into it’s format means with 3 dots (.), we need to use INET_NTOA. INET_NTOA is a MySQL function which converts number to ip address. This is the use of INET_ATON and INET_NTOA mysql functions. If we are looking for PHP functions which works same as MySQL functions for IP address.


1 Answers

This tutorial might help you.

The most efficient way of saving IPv4 addresses is with an INT field (not VARCHAR as you might expect). You convert them using PHP's ip2long and back using either MySQL's INET_NTOA function or PHP's long2ip function.

If you need to store IPv6, you'll want to use a BINARY field instead and PHP's inet_pton function.

like image 51
Francois Deschenes Avatar answered Sep 19 '22 03:09

Francois Deschenes