0

I'm having a problem writting an IP to the database. If I print $ip after $ip=$_SERVER['REMOTE_ADDR'];, I obtain 54.231.128.128. But if I insert ip to database, it inserts 203223. Here is my code.

<?php include "db.php";
session_start();
$toemail=$_POST["toemail"];
$report=$_POST["report"];
$ip=$_SERVER['REMOTE_ADDR'];
$fromemail=$_SESSION['nahid'];
$date=(time());
$tatus="Active";

if(!$_POST['submit'])   {
    echo "Please fill out the form";

    }   else    {

    // $query_auto = "INSERT INTO form (date, time) VALUE ('DATE: Auto CURDATE()', CURDATE() )";


mysql_query("INSERT INTO report(`id`, `from`, `msg`, `to`, `date`, `ip`, `status`) 
            VALUES(NULL,'$fromemail','$report','$toemail','$date', '$ip', '$tatus')") or die(mysql_error());
    header('Location: report_congratulations.php');
}

?> 
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • 2
    what type is the IP column in your database? –  Jan 12 '15 at 11:14
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 12 '15 at 11:15
  • ip column type should be varchar, or you could use php function `ip2long` to convert ip to number, and when retrieving use `long2ip` to convert back to ip – Aleksandar Vasić Jan 12 '15 at 11:17
  • 1
    http://stackoverflow.com/questions/5133580/which-mysql-datatype-for-an-ip-address – fortune Jan 12 '15 at 11:17

2 Answers2

0

REFERENCE INET_ATON and INET_NTOA

mysql_query("INSERT INTO report(`id`, `from`, `msg`, `to`, `date`, (INET_ATON('$ip')), `status`) 
        VALUES(NULL,'$fromemail','$report','$toemail','$date', '$ip', '$tatus')") or      die(mysql_error());
Priyank
  • 3,778
  • 3
  • 29
  • 48
0

Problem solved. My Database IP column was integer. But now I converted varchar and problem has solved.