通過配置實現電腦和手機訪問分別顯示不同的頁面(以thinkphp5為例)
阿新 • • 發佈:2019-01-22
在thinkphp5入口檔案public/index.php下面加入以下程式碼即可:
function is_mobile()
{
// getallheaders();//如果是apache可以直接用這個函式獲得請求頭
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
$is_pc = (strpos($agent, 'windows nt'));
$is_mac = (strpos($agent, 'mac os'));
$is_iphone = (strpos($agent, 'iphone'));
$is_android = (strpos($agent, 'android'));
$is_ipad = (strpos($agent, 'ipad'));
if($is_pc){define('BIND_MODULE','admin');}
if($is_mac){define('BIND_MODULE','admin');}
if($is_iphone){define('BIND_MODULE','index');}
if($is_android){define('BIND_MODULE','index');}
if($is_ipad){define('BIND_MODULE','index');}
}
is_mobile();
BIND_MODULE是控制繫結模組的,就是說,輸入同樣的網址IP,訪問不同的模組用的!