简体   繁体   English

在MySQL中存储IP地址?

[英]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" 我试图将用户的IP地址存储到VARCHAR(39)下的mysql表中,但其存储方式仅为"::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列更改为varbinary (16)数据类型,然后使用以下命令存储ips:

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

Note: make sure your php version is up to date as inet_pton() is a new function. 注意:确保您的php版本是最新的,因为inet_pton()是一个新函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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