في هذه المرحلة سنقوم بجلب بيانات المتصفحين وحفظها في الجدول المخصص لذلك ..
قمت بتقسيم الكود الخاص بهذه المرحلة إلى 4 أقسام رئيسية , بحيث يقوم كل قسم بعمل خطوة معينة من خطوات هذه المرحلة وهي كالتالي:
1- المرحلة 1 جلب رقم الأيبي و تحليل الدولة
الكود لهذه المرحلة هو
رمز Code:
//====== Part 1 : check and get ip details of the visitor======== if(!isset($_SESSION['IpAddress'])){ //*******************************************First get the Real IP Address of the visitors==== $useragent = $_SERVER['HTTP_USER_AGENT'];// get system info function getRealIPAddress(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; }else if(!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; } $_SESSION['IpAddress']=getRealIPAddress(); //====================================// //***************************************** get the details of the IP address function getCountryFull(){ $ip= getRealIPAddress(); // call the function of Geiting The Ip to get the IP real one $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip)); if($ip_data && $ip_data->geoplugin_countryName != null){ $country= $ip_data->geoplugin_countryName; } return $country; } //___---+++ Because some servers doesn't support the pervious code, the next one may work better function getLocationInfoByIp($ipaddress){ $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ipaddress)); if($ip_data && $ip_data->geoplugin_countryName != null){ $country= $ip_data->geoplugin_countryName; } return $country; } function getCountry(){ $ip= getRealIPAddress(); // call the function of Geiting The Ip to get the IP real one $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip)); if($ip_data && $ip_data->geoplugin_countryName != null){ $country= $ip_data->geoplugin_countryCode; } return $country; } //====================================// if (getCountryFull() != null){ $_SESSION['CountryName']=getCountryFull(); } else{ if (getLocationInfoByIp($ip) != null){ $_SESSION['CountryName']=getLocationInfoByIp($ip); } else{ echo "</br>Oooh No the 2 second code is Null"; } } if (getCountry() != null){ $_SESSION['CountryCode']=getCountry(); } else{ if (getLocationInfoByIp2($ip) != null){ $_SESSION['CountryCode']=getLocationInfoByIp2($ip); } else{ echo "</br>Oooh No the 2 second code is Null"; } } } /// end of checking of Session ipaddress //================= End of Part 1 : Albadani Network =====================
وشرح الكود كالتالي:
أولا نقوم بمعرفة الأيبي للزائر , ومعرفة هل هو أيبي حقيقي أم لا عبر الكود هذا
رمز Code:
if(!isset($_SESSION['IpAddress'])){
//*******************************************First get the Real IP Address of the visitors====
$useragent = $_SERVER['HTTP_USER_AGENT'];// get system info
function getRealIPAddress(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}else if(!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;
}
$_SESSION['IpAddress']=getRealIPAddress();
ملاحظة: عملية التحليل ليست بهذه السهولة , لذا تقدم الكثير من المواقع خدمة تحليل الأيبي لمن يرغب بذلك , وهذه المواقع كثيرة وانا قد اخترت لكم احدها , ولكم الحق في اختيار أي موقع اخر أو طرق أخرى حسب رغبتكم
الموقع المستخدم لهذه العملية هو geoPlugin to geolocate your visitors
وللقيام بالتحليل تم اضافة functions لجلب بيانات الدولة , الأولى لجلب الاسم الكامل للدولة , والآخر لجلب الكود الخاص بالدولة , مثلا اليمن اسمها الكامل Yemen والكود تبعها هو Ye وهكذا
رمز Code:
//***************************************** get the details of the IP address
function getCountryFull(){
$ip= getRealIPAddress(); // call the function of Geiting The Ip to get the IP real one
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null){
$country= $ip_data->geoplugin_countryName;
}
return $country;
}
//___---+++ Because some servers doesn't support the pervious code, the next one may work better
function getLocationInfoByIp($ipaddress){
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ipaddress));
if($ip_data && $ip_data->geoplugin_countryName != null){
$country= $ip_data->geoplugin_countryName;
}
return $country;
}
function getCountry(){
$ip= getRealIPAddress(); // call the function of Geiting The Ip to get the IP real one
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null){
$country= $ip_data->geoplugin_countryCode;
}
return $country;
}
//====================================//
رمز Code:
if (getCountryFull() != null){
$_SESSION['CountryName']=getCountryFull();
} else{
if (getLocationInfoByIp($ip) != null){
$_SESSION['CountryName']=getLocationInfoByIp($ip);
} else{
echo "</br>Oooh No the 2 second code is Null";
}
}
if (getCountry() != null){
$_SESSION['CountryCode']=getCountry();
} else{
if (getLocationInfoByIp2($ip) != null){
$_SESSION['CountryCode']=getLocationInfoByIp2($ip);
} else{
echo "</br>Oooh No the 2 second code is Null";
}
}
} /// end of checking of Session ipaddress
//================= End of Part 1 : Albadani Network =====================
رمز Code:
//====== Part 2 : make var to save the data that you want ================ $date=date("d/m/Y"); $time=date("H:i:s"); $ipaddress=$_SESSION['IpAddress']; $CountryName=$_SESSION['CountryName']; $CountryCode=$_SESSION['CountryCode']; echo $date."</br>".$time."</br>".$ipaddress."</br>".$CountryName."</br>".$CountryCode; //================= End of Part 2 : Albadani Network =====================
أما الثالثة فهي للاتصال بالقاعدة عبر ملف الاتصال cinfg.php كما يلي , مع ملاحظة تغيير مسار المجلد للوصول إلى الملف الصحيح..
رمز Code:
//====== Part 3 : call confg file to make connection to db ===============
require_once("include/confg.php");
//================= End of Part 3 : Albadani Network =====================
وأخيراً الحفظ في قاعدة البيانات .. حيث في البداية يتم التأكد من الآيبي , هل قد تم حفظ بياناته لهذا اليوم في القاعدة سابقاً .. فإذا كان كذلك فيتم الاكتفاء بتعديل بيانات أخر وقت للزيارة و أيضاً عدد الصفحات التي تم تصفحها, وأخر صفحة تم زياراتها , كما في الكود التالي:
رمز Code:
//====== Part 4 : save the process to the database ===============
if($connect){
$sql="select * from visits_info where date='$date' and ip_address='$ipaddress'";//check does the ip visi us today?
$query=mysqli_query($connect,$sql);
$rows=mysqli_num_rows($query);
if($rows==0 ){
$sql="INSERT INTO visits_info (ip_address,system_info,country_name,country_code,date,time,last_time,pages_view,day,month,year,current_place) values('$ipaddress','$useragent','$CountryName','$CountryCode','$date','$time','$time','1','$dd','$mm','$yy','$what_page')";
$query=mysqli_query($connect,$sql);
}
else{
$sql="update visits_info set pages_view=pages_view+1 , last_time='$time' , user_id='$userId' , pervious_place=current_place , current_place='$what_page' where date='$date' and ip_address='$ipaddress'";
$query=mysqli_query($connect,$sql);
}
}
//================= End of Part 4 : Albadani Network =====================
في النهاية يصبح الكود الكامل لهذه العملية كما يلي
رمز Code:
<?php //====== Part 1 : check and get ip details of the visitor======== if(!isset($_SESSION['IpAddress'])){ //*******************************************First get the Real IP Address of the visitors==== $useragent = $_SERVER['HTTP_USER_AGENT'];// get system info function getRealIPAddress(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; }else if(!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; } $_SESSION['IpAddress']=getRealIPAddress(); //====================================//

ليست هناك تعليقات:
اضافة تعليق