I have this code

<?php
    // Connect to the database
    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }


    // Get the user's IP address
    $user_r_ip = $_SERVER['REMOTE_ADDR'];

    function getUserIpAddr(){
        if(!empty($_SERVER['HTTP_CLIENT_IP'])){
            //ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            //ip pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }else{
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }



    //Get User Location details
    $user_ip = getUserIpAddr();
    $geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
    $country = $geo["geoplugin_countryName"];

    // Get the URL parameters
    $amount = $_COOKIE['amount'];
    $btc = $_COOKIE['btcadd'];


    if (isset($_COOKIE['btcadd']) && isset($_COOKIE['amount'])) {
        // Prepare the INSERT statement
        $stmt = mysqli_prepare($conn, "INSERT INTO btcs (btc, amount, country, ip) VALUES (?, ?, ?, ?)");

        // Bind the parameters
        mysqli_stmt_bind_param($stmt, "ssss", $btc, $amount, $country, $user_r_ip);

        // Execute the statement
        mysqli_stmt_execute($stmt);

        // Close the statement
        mysqli_stmt_close($stmt);

        // Close the connection
        mysqli_close($conn);
    }
    echo $country;
?>

and i have this code

<?php
    // Connect to the database
    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }

    // Get the user's IP address
    $user_r_ip = $_SERVER['REMOTE_ADDR'];

    function getUserIpAddr(){
        if(!empty($_SERVER['HTTP_CLIENT_IP'])){
            //ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            //ip pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }else{
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    // Get the URL parameters
    $btc = $_POST['btcadd'];
    $amount = $_POST['amount'];

    //Get User Location details
    $user_ip = getUserIpAddr();
    $geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
    $country = $geo["geoplugin_countryName"];

    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['btcadd']) && isset($_POST['amount'])) {
      // Prepare the INSERT statement
      $stmt = mysqli_prepare($conn, "INSERT INTO btcs (btc, amount, country, ip) VALUES (?, ?, ?, ?)");

      // Bind the parameters
      mysqli_stmt_bind_param($stmt, "ssss", $btc, $amount, $country, $user_r_ip);

      // Execute the statement
      if (!empty($btc)) {
        mysqli_stmt_execute($stmt);
        echo "btc address empty";
        //mysqli_stmt_close($stmt);
      } else {
        echo "<center>.</center>";
        mysqli_close($conn);
        $conn-exit();
      }
    }

?>

Why on the first code the user location is wrong? I am using COOKIES
And on the second code the user location is correct? i am using POST

And is there a way i get the correct location of the user on the first example? BTW the user IP's are correct in the both of the examples, only the country name is wrong on the first example

Recommended Answers

All 3 Replies

I'm not spotting the difference between the two code blocks you have here other than, in the first one, you echo $country and in the second one you don't.

Also, in both of them you seem to have the line $user_r_ip = $_SERVER['REMOTE_ADDR']; but you are not using that variable anywhere. In both cases, the IP address passed into geoplugin.net is based on getUserIpAddr().

Yeah i just removed that function and now i am using only $user_ip = $_SERVER['REMOTE_ADDR'];
It works flawless and i dont see any wrong using it like this. I was figuring out whole day what is wrong with the above code and i was not able to figure it out so i guess this is the best solution :D

The reason for using the other method is if the end-user is using a proxy server. The method you have in getUserIpAddr() is actually the more thorough way of getting a user's IP address. By any chance are you utilizing a proxy server?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.