abdallah al sawaqi
abdallah al sawaqi

Reputation: 73

Information not saved in the database

I have created a query which should store the ip address,browser & country of any user who is viewing the website, but the information is not being stored in the database.

Here is the full code.

Connection

<?php
$conn = mysqli_connect("localhost","root","","ecom_store");

Function to get IP

function getRealUserIp(){
  switch(true){
    case(!empty($_SERVER['HTTP_X_REAL_IP'])) : 
      return $_SERVER['HTTP_X_REAL_IP'];
    case(!empty($_SERVER['HTTP_CLIENT_IP'])) : 
      return $_SERVER['HTTP_CLIENT_IP'];
    case(!empty($_SERVER['HTTP_X_FORWARD_FOR'])) : 
      return $_SERVER['HTTP_X_FORWARD_FOR'];
    default : return $_SERVER['REMOTE_ADDR'];
  }
}

Function to get Users Browser

function getBrowser(){
  $agent = $_SERVER['HTTP_USER_AGENT'];
  $name = 'NA';
  if (preg_match('/MSIE/i', $agent) && !preg_match('/Opera/i', $agent)) {
    $name = 'Internet Explorer';
  } elseif (preg_match('/Firefox/i', $agent)) {
    $name = 'Mozilla Firefox';
  } elseif (preg_match('/Chrome/i', $agent)) {
    $name = 'Google Chrome';
  } elseif (preg_match('/Safari/i', $agent)) {
    $name = 'Apple Safari';
  } elseif (preg_match('/Opera/i', $agent)) {
    $name = 'Opera';
  } elseif (preg_match('/Netscape/i', $agent)) {
    $name = 'Netscape';
  }
  return $name;
}

Function to get user country

function ip_visitor_country()
{
  $client  = @$_SERVER['HTTP_CLIENT_IP'];
  $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  $remote  = $_SERVER['REMOTE_ADDR'];
  $country  = "Unknown";

  if(filter_var($client, FILTER_VALIDATE_IP))
  {
    $ip = $client;
  }
  elseif(filter_var($forward, FILTER_VALIDATE_IP))
  {
    $ip = $forward;
  }
  else
  {
    $ip = $remote;
  }
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://www.geoplugin.net/json.gp?ip=".$ip);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $ip_data_in = curl_exec($ch); // string
  curl_close($ch);

  $ip_data = json_decode($ip_data_in,true);
  $ip_data = str_replace('&quot;', '"', $ip_data);  

  if($ip_data && $ip_data['geoplugin_countryName'] != null) {
    $country = $ip_data['geoplugin_countryName'];
  }

  return $country;
}

Function to save the information

function save(){
  global $conn;
  $c_ip= getRealUserIp();

  $c_browser= getBrowser();

  $c_country= ip_visitor_country();

  $insert_save="insert into ip (c_ip,c_browser,c_country) 
                values('$c_ip,'$c_browser','$c_country')";

  $run_save = mysqli_query($conn,$insert_save);

  echo $insert_save;

  if($run_save){
    echo "saved";
  }else{
    echo"error";
  }
}

?>

<?php save();?>

All my code is in written in one page. Thank you

Upvotes: 1

Views: 85

Answers (1)

Abdallah
Abdallah

Reputation: 198

You forgot to add an apostrophe after $c_ip

$insert_save="insert into ip (c_ip,c_browser,c_country) 
            values('$c_ip','$c_browser','$c_country')";

Upvotes: 2

Related Questions