1. 程式人生 > 實用技巧 >php CI框架判斷瀏覽器跨域訪問頭部資訊,PC、手機版本程式碼!

php CI框架判斷瀏覽器跨域訪問頭部資訊,PC、手機版本程式碼!

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|	https://codeigniter.com/user_guide/general/hooks.html
|
*/

/** 跨域訪問頭部資訊,PC、手機版本判斷 */
$hook['post_controller_constructor'][] = function () {
    $CI =& get_instance();

    /** 跨域訪問頭部資訊 */
    $origin = $CI->input->server('HTTP_ORIGIN', TRUE);
    if ($origin && preg_match('@^http(s)?://(.*)\.tzb\.me$@i', $origin))
    {
        @header("Access-Control-Allow-Credentials: true");
        @header('Access-Control-Allow-Origin:' . $origin);
        @header('Access-Control-Allow-Methods:GET,POST');
    }

    define('IN_ADMIN', (ADMINPATH == $CI->uri->segment(1)));

    /** 瀏覽器 */
    $CI->load->library('user_agent');
    define('IN_MOBILE', $CI->agent->is_mobile());
    if (IN_ADMIN === FALSE)
    {
        /** 定義對應模板目錄 */
        define('MOBILE_TPL', (IN_MOBILE ? '/mobile' : ''));
        define('TEMPLET_DIR', trim(config_item('templet'), '/'));

        /** 判斷客戶端,1電腦,2手機,3小程式 */
        $referer = $CI->input->server('HTTP_REFERER', TRUE);
        if (stripos($referer, 'https://servicewechat.com/') === 0)
        {
            $client = 3;
        }
        else
        {
            $client = (IN_MOBILE ? 2 : 1);
        }
        define('USE_CLIENT', $client);  //當前使用的客戶端
    }
    else
    {
        define('USE_CLIENT', 1); //後臺預設是PC
    }
};

  很多人使用到php的時候不知道怎麼判斷瀏覽器來路、手機版本資訊等,現在我們整理一份建議收藏!如果不知道怎麼開發可以訪問網站:https://www.hu-ling.cn 找到技術進行二開。