http://go.oclasrv.com/afu.php?zoneid=1520134 Mimo-Tech: لمعرفة بيانات زوار موقعك و تتبع حركة تصفحهم للموقع و عمل صفحة المتواجد في الموقع لمعرفة بيانات زوار موقعك و تتبع حركة تصفحهم للموقع و عمل صفحة المتواجد في الموقع | Mimo-Tech

الاثنين، 1 يناير 2018

لمعرفة بيانات زوار موقعك و تتبع حركة تصفحهم للموقع و عمل صفحة المتواجد في الموقع

                                

في هذه المرحلة سنقوم بجلب بيانات المتصفحين وحفظها في الجدول المخصص لذلك ..
قمت بتقسيم الكود الخاص بهذه المرحلة إلى 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 =====================
2- المرحلة الثانية هي لحفظ المتغيرات المطلوبة كما يلي
رمز 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();
//====================================//

ليست هناك تعليقات:

اضافة تعليق

جميع الحقوق محفوظة © 2013 Mimo-Tech
تصميم : يعقوب رضا