ALK
ALK

Reputation: 103

saving IP address in mysql database

I have a problem with saving the IP address into DataBase

This is how i get the ip:

function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}


$user_ip = getUserIP();

That is my DB Table creation php file

$sql =  "CREATE TABLE users (".
            "ID         INT     NOT NULL    AUTO_INCREMENT,".
            "Guest          VARCHAR(60)     COLLATE utf16_general_ci,".
                        "IPAdd          INT UNSIGNED    NULL            DEFAULT NULL,".
            "PRIMARY KEY(ID));";

    $retval = mysql_query( $sql, $conn );

End this is how i save the Users IP

require("DBConnection.php");
require("getIP.php");

echo $user_ip."<br>";
if ($user_ip === '::1'){
    $user_ip = '127.0.0.1';
}
$user_ip = ip2long ($user_ip);
echo $user_ip."<br>";
//Проверява ме за вече съществъващ гост
$selectData  = mysql_query("SELECT * FROM users"); 
if (!$selectData ) 
{
        die('Could not get data: ' . mysql_error());
}
else {
    $i = 1; // index za poreden nomer na potrebitelq.
    $guest = 'Guest'.$i;
    while ($row = mysql_fetch_array($selectData) )
    {
        if($row[1] == $guest && $row[2] != $user_ip )
        {
            $i++;
            $guest = 'Guest'.$i;
        }     
    }
    $sql = "INSERT INTO `cssgendb`.`users` (`ID`, `Guest`, 'IPAdd') 
            VALUES ('0', '$guest', '$user_ip');";
    $_SESSION['is_logged'] = true;
    $_SESSION['Name'] = $guest;
}
$retval = mysql_query( $sql, $conn );
if(! $retval )
    {
      die('Could not enter data: ' . mysql_error());
    }
    echo "Session Start successfully\n";    
mysql_close($conn);

My Idea is to get users IP and save it. If the user IP doesn`t exist in DataBase the Account will be created like "Guest1"... IPAdd "127.0.0.1";

Upvotes: 0

Views: 1632

Answers (3)

Syed Arif Iqbal
Syed Arif Iqbal

Reputation: 1427

change ipadd field to (int) and use $_SERVER['SERVER_ADDR'] to get user system ip address..

Upvotes: 0

motanelu
motanelu

Reputation: 4025

You have 2 choices:

  1. change the datatype of the IPAdd column from Unsigned Int to Varchar to save the ip in string format
  2. convert the IP into long format before saving it by calling ip2long

As previously stated by another person, you should consider switching from mysql* to either mysqli or PDO_MySQL as the mysql* functions are deprecated.

Upvotes: 1

Ram Sharma
Ram Sharma

Reputation: 8819

You need to change the datatype of IPAdd, You can not save data like 127.0.0.1 or any IP address in integer field. You can save only positive or negative integer value without . in this field

change your datatype to varchar like this

ALTER TABLE `users` CHANGE `IPAdd` `IPAdd` VARCHAR( 20 ) NULL

Upvotes: 0

Related Questions