分析浏览器脚本

    //Analysis browser

    function getBrowser($agent){
        if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
            return "ie";
        else if(strpos($agent,'Firefox')!==false)
            return "firefox";
        else if(strpos($agent,'Chrome')!==false)
            return "chrome";
        else if(strpos($agent,'Opera')!==false)
            return 'opera';
        else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
            return 'safari';
        else if((strpos($agent,'AppleWebKit')!==false))
            return 'safari';
        else
            return 'unknown';
    }

    function getBrowserVer($agent){
        if (preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs))
            return $regs[1];
        elseif (preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs))
            return $regs[1];
        elseif (preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs))
            return $regs[1];
        elseif (preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs))
            return $regs[1];
        elseif ((strpos($agent,'Chrome')==false)&&preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs))
            return $regs[1];
        elseif (preg_match('/AppleWebKit\/(\d+)\..*/i', $agent, $regs))
            return $regs[1];
        elseif (preg_match('/Trident\/(\d+)\..*/i', $agent, $regs))
            return $regs[1];
        else
            return 'unknow';
    }

    function get_ie_model($agent){
        if(strpos($agent,'compatible')!==false){
            return 'compatible';
        }else{
            return '';
        }
    }

    function getos($agent){
        if(strpos($agent,'Macintosh')!==false){
            return 'Mac';
        }
        else if(strpos($agent,'Android')!==false){
            return 'Android';
        }
        else if(strpos($agent,'Windows')!==false){
            return 'Windows';
        }
        else if(strpos($agent,'iPad')!==false){
            return 'iPad';
        }
        else if(strpos($agent,'iPhone')!==false){
            return 'iPhone';
        }
        else {
            return 'unknow';
        }
    }

    function transfer_browser_info(){
        ini_set('max_execution_time', '0');

        //get customer lkup arr
        $customer_list = $this->db->query("select customer_id, customer_name from evo_central_config.customer")->result_array();
        $customer_lkup = array();
        foreach($customer_list as $row){
            $customer_lkup[$row['customer_id']] = $row['customer_name'];
        }

        $sql = "select count(*) as cnt, login_name, user_agent from evo_central.user_login_trail where login_time > '2015-01-01 00:00:00' and login_password = 'OK' and login_name != '' group by login_name, user_agent" ;
        $result = $this->db->query($sql)->result_array();

        foreach($result as $row)
//        $query = $this->db->query($sql);
//        while ($row = $query->unbuffered_row())
        {
            $login_name = $row['login_name'];
            $agent = $row['user_agent'];

            #user_id,user_name,customer_id,customer_name,os,browser,version,model
            $user_info = $this->db->query("select * from evo_central_config.customer_user_online where login_name = '$login_name'")->row_array();

            $insert_arr = array();
            if(!empty($user_info)){
                $insert_arr['cnt']           = $row['cnt'];
                $insert_arr['user_id']       = $user_info['user_id'];
                $insert_arr['user_name']     = $user_info['user_name'];
                $insert_arr['customer_id']   = $user_info['customer_id'];
                $insert_arr['customer_name'] = !empty($customer_lkup[$insert_arr['customer_id']]) ? $customer_lkup[$insert_arr['customer_id']] : '' ;
                $insert_arr['os']            = $this->getos($agent);
                $insert_arr['browser']       = $this->getBrowser($agent);
                $insert_arr['version']       = $this->getBrowserVer($agent);
                $insert_arr['model']         = $this->get_ie_model($agent);
                $insert_arr['agent']         = $agent;

                $this->db->insert('evo_central.test',$insert_arr);
            }

        }
    }