I want to insert my visitor's IP info into my mysql table, i'm using jQuery Post method to do this. here is my code
Main_page.php
<?php
if(!isset($_SESSION['v_Id']) || empty($_SESSION['v_Id'])) {
echo '<script>
$.get("https://ipapi.co/json", function (response) {
/*
var ip = response.ip,
country = response.country_name,
isp = response.org,
timezone = response.timezone,
calling_code = response.country_calling_code
language = response.languages;
alert(ip);
alert(country);
alert(isp);
alert(language);
alert(calling_code);
alert(timezone);
*/
$.post("./saveData", {
ip: response.ip,
country: response.country_name,
isp: response.org,
timezone: response.timezone,
calling_code: response.country_calling_code,
language: response.languages
}, function(data, status){
console.log("Data: " + data + "\nStatus: " + status);
});
});
</script>';
}
?>
saveData.php
<?php
include 'config.php';
$date = date('Y-m-d H:i:s', time());
// from my assets class to generate a unique string
$Id = $assets->randomString(15, 1, "upper_case,numbers");
$id = $Id[0];
$form_id = filter_var($_POST['form_id'], FILTER_SANITIZE_STRING);
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$ip = filter_var($_POST['ip'], FILTER_SANITIZE_STRING);
$country = filter_var($_POST['country'], FILTER_SANITIZE_STRING);
$isp = filter_var($_POST['isp'], FILTER_SANITIZE_STRING);
$calling_code = filter_var($_POST['calling_code'], FILTER_SANITIZE_STRING);
$timezone = filter_var($_POST['timezone'], FILTER_SANITIZE_STRING);
$t_ins = $conn->prepare("INSERT INTO visits(ip, country, isp, calling_code, timezone, time, visit_id) VALUES(?, ?, ?, ?, ?, ?, ?)");
$t_ins->bind_param("sssssss", $ip, $country, $isp, $calling_code, $timezone, $date, $id);
if($t_ins->execute()){
$_SESSION["v_Id"] = $id;
}
?>
My PHP code is working fine but every time when I refresh my Main_page.php, js code is executing even when Session
is set.