获取操作系统信息

时间:2020-07-07 21:53:12

标签: php

嘿,我过度使用了Get operating system info (EDIT the original code was from https://github.com/Jacckii/-PHP-Simple-IP-Logger)中的代码,但是我遇到了两个错误:

  1. 警告:preg_match():第109行的C:\ Users \ User \ final.php中的未知修饰符'5' (EDIT Thanks to https://stackoverflow.com/users/1491895/barmar he fixed the 1. Error)
  2. 警告:fclose()期望参数1为资源,int在第154行的C:\ Users \ User \ final.php中给出 (EDIT fixed by removing fclose()

最终:

<?php

// Getting IP
session_start();
if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
    $res = ' | Resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height'];
} else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) {
    $_SESSION['screen_width'] = $_REQUEST['width'];
    $_SESSION['screen_height'] = $_REQUEST['height'];
    header('Location: ' . $_SERVER['PHP_SELF']);
} else {
    echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
}

if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
      $ipaddress = $_SERVER['HTTP_CLIENT_IP']."";
    }
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
      $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']."";
    }
else
    {
      $ipaddress = $_SERVER['REMOTE_ADDR']."";
    }
// END of Getting IP
//
// Getting User-Agent
$user_agent     =   $_SERVER['HTTP_USER_AGENT'];
// END of Getting User-Agent
//
// Getting OS Name
function getOS() { 

    global $user_agent;

    $os_platform    =   "Unknown OS Platform";

    $os_array       =   array(
                            '/windows nt 10/i'      =>  'Windows 10',
                            '/windows nt 6.3/i'     =>  'Windows 8.1',
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/cros/i'               =>  'Chrome OS',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                                          '/kalilinux/i'          =>  'KaliLinux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile',
                                          '/Windows Phone/i'      =>  'Windows Phone'
                            );

    foreach ($os_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }

    }   

    return $os_platform;

}
// END of Getting OS Name
//
// Getting Browser
function getBrowser() {

    global $user_agent;

    $browser        =   "Unknown Browser";

    $browser_array  =   array(
                            '/msie/i'                     =>  'Internet Explorer',
                            '/firefox/i'                  =>  'Firefox',
                            '/Mozilla/i'                    =>  'Mozila',
                            '/Mozilla\/5\.0/i'              =>  'Mozila',
                            '/safari/i'                   =>  'Safari',
                            '/chrome/i'                   =>  'Chrome',
                            '/edge/i'                     =>  'Edge',
                            '/opera/i'                    =>  'Opera',
                                          '/OPR/i'                      =>  'Opera',
                            '/netscape/i'                 =>  'Netscape',
                            '/maxthon/i'                  =>  'Maxthon',
                            '/konqueror/i'                =>  'Konqueror',
                                          '/Bot/i'                        =>    'BOT Browser',
                                          '/Valve Steam GameOverlay/i'  =>  'Steam',
                            '/mobile/i'                   =>  'Handheld Browser'
                        );

    foreach ($browser_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $browser    =   $value;
        }

    }

    return $browser;

}
// END Getting Browser
//
//Setting Os and Browser Variables
$user_os        =   getOS();
$user_browser   =   getBrowser();
//
// Getting visitor IP address
$ip = $_SERVER['HTTP_CLIENT_IP'];
// Getting where visitor comes from (URL)
$site_refer = $_SERVER['HTTP_REFERER'];
    // Check if hes connected directly 
    if($site_refer == ""){
        $site = "dirrect connection";
    }
  // If he isn't
    else{
        $site = $site_refer;
    }
  // Hide Owner's IP address
    $owner = "HIDE THIS IP ADDRESS";   //Change $owner for something else, cuz someone can simple read that by calling out $owner
  // change it for $absdfs5sd4 for example and change it down there 
    $owner_country = "YOUR COUNTRY TAG FOR YOUR IP ↑";
  
    if($ip == $owner){
        $ip = "Owner"; 
        $country = $owner_country;
    }
  //Find info about IP address
    else{
        $details = json_decode(file_get_contents("http://ipinfo.io/{$ipaddress}"));
    $country = $details->country;
    $region = $details->region;
    $city = $details->city;
  }
    // Writing in to txt file
 file_put_contents('ips.txt',"Date : ". date("Y-m-d - H:i:s"). " | IP: ". $ipaddress. " | Country: ". $country. " | Region: ". $region. " | City: ". $city. " | OS: ". $user_os. " | Browser: ". $user_browser. $res. " | Come from site: ". $site. " | User-Agent: " .$user_agent .PHP_EOL , FILE_APPEND | LOCK_EX);
?>

0 个答案:

没有答案