简体   繁体   中英

storing ip address in mysql?

i am trying to store a users ip address into a mysql table under VARCHAR (39) but its storing as just this "::1"

i am using this code:

<?php
session_start();

$db_hostname = 'localhost';
$db_database = 'hewden1'; 
$db_username = 'root';
$db_password = '';

$db_server = mysql_connect($db_hostname, $db_username, $db_password)    
        or die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database)   
or die("Unable to select database: " . mysql_error());

 $cname       = $_POST['cname'];
   $creg      = $_POST['creg'];  
   $address      = $_POST['address'];  
   $post      = $_POST['post'];  
   $contactn      = $_POST['contactn'];
   $contactt      = $_POST['contactt'];
   $email      = $_POST['email'];
   $vat     = $_POST['vat'];

    $ipaddress = $_SERVER["REMOTE_ADDR"];


$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";$result = mysql_query($sql); 

if($result){

echo "jobs a gooden";

}else {
echo "ERROR";
}
?>

can someone please show me where i am going wrong thanks

Simple solution, change your ip column to varbinary (16) datatype and then store ips using the following:

$ip = bin2hex(inet_pton($_SERVER['REMOTE_ADDR']));

Note: make sure your php version is up to date as inet_pton() is a new function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM