0

Bear with me as i will try my best to explain the scenario.i have database for my website which contains visitors information.I have been testing this database and website using local Host with different browsers such as Firefox,IE and GoogleCrome. The database works fine as i am saving "time","IP address","browser name" and "country" of the visitor.Now that i have hosted this website on a domain,Database works fine for Firefox,IE..but whenever the website is accessed from GoogleCrome,there is no entry in the database for that.I am using XAMPP SQL database for testing

Here is the code of that

<?php
function get_browser_name($user_agent)
{
    if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 
'Opera Browser';
    elseif (strpos($user_agent, 'Edge')) return 'Edge';
    elseif (strpos($user_agent, 'Chrome')) return 'Google Chrome';
    elseif (strpos($user_agent, 'Safari')) return 'Safari';
    elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
    elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) 
        return 'Internet Explorer';

    return 'Other';
}

$agent=get_browser_name($_SERVER['HTTP_USER_AGENT']);
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp? 
ip=".getRealIpAddr());
$user ='root';
$pass ='';
$db ='simplecounter';


$db = new mysqli('localhost', $user, $pass, $db) or die("unable to 
     connect");


$find_counts = mysqli_query($db,"SELECT * FROM user_count");
$ip=$_SERVER['REMOTE_ADDR'];
$date = new DateTime("now", new DateTimeZone($xml- 
>geoplugin_timezone) );
$timee= $date->format('Y-m-d H:i:s');

$sql = "INSERT INTO user_count 
                (browser_name, ip_address, visiting_time, country)
        VALUES ( '.$agent.', '.$ip.', '.$timee.', 
                '.$xml->geoplugin_countryName.')";
if ($db->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


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

Unable to find the root reason for the problem ,any help would be great.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 2
    Offtopic: this `getRealIpAddr()` function is BAD because if is spoofable if i send a HTTP_CLIENT_IP header i can override mine real IP – Raymond Nijland Jul 02 '19 at 13:14

2 Answers2

1

Offtopic: this getRealIpAddr() function is BAD because if is spoofable if i send a HTTP_CLIENT_IP header i can override mine real IP

This is to long for a comment. but i advice you to replace getRealIpAddr() function with.

function getClientIPAddresses() {
   $ips = array();
    
   (isset($_SERVER['HTTP_CLIENT_IP'])) ? $ips["HTTP_CLIENT_IP"] = $_SERVER['HTTP_CLIENT_IP'] : null; 
   #... add the other headers cases as well. 

   (isset($_SERVER['REMOTE_ADDR'])) ? $ips["REMOTE_ADDR"] = $_SERVER['REMOTE_ADDR'] : null; 
    
   return $ips;
}

As this function will return a array with IP's which the client uses.

Keep in mind X-Forwarded-For header can use a comma separated IP list and there is a RFC 7239 ( Forwarded HTTP Extension) for it as well..
So correctly implementing will require more research and code.

Community
  • 1
  • 1
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
0

I managed to get your code working on my localhost, but i had to replace the getRealIpAddr function with the function in this answer: https://stackoverflow.com/a/13646735/11724378

Also be mindful of the fact that you redeclare the $ip variable lower in your code as simply $_SERVER['REMOTE_ADDR'], which could also lead to problems. Might as well declare it before loading the xml, like so:

$ip = getRealIpAddr();
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=" . $ip);

Google Chrome got successfully inserted into the table on my end. Check if you have any restrictions for that variable length: is "Internet Explorer" saved correctly? Maybe some other SQL error pops up? If nothing else works, try var_dumping out your sql command, and experimenting with it via PhpMyAdmin.

Andrew
  • 827
  • 2
  • 6
  • 14