관리 메뉴

웹개발자의 기지개

[ php ] 모바일 인지 pc 환경인지 구분함수 본문

PHP

[ php ] 모바일 인지 pc 환경인지 구분함수

http://portfolio.wonpaper.net 2020. 9. 17. 10:01
function is_mobile(){
	if(preg_match('/(alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|sagem|sharp|sie-|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte)/i', $_SERVER['HTTP_USER_AGENT'])){

		return true;
	} else { 
		return false;
	}
}

 

$mobile_agent = "/(iPod|iPhone|Android|BlackBerry|SymbianOS|SCH-M\d+|Opera Mini|Windows CE|Nokia|SonyEricsson|webOS|PalmOS)/";

if(preg_match($mobile_agent, $_SERVER['HTTP_USER_AGENT'])){
	echo "Mobile";
}else{
	echo "PC";
}

 

 

if (!function_exists('str_contains')) {
	function str_contains($haystack, $needle) {
		if ('' === $needle) {
			return true;
		}
		return false !== strpos($haystack, $needle);
	}
}

function is_mobileTest() {
    if (isset($_SERVER['HTTP_SEC_CH_UA_MOBILE'])) {
        return ( '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ); //
    } elseif (!empty($_SERVER['HTTP_USER_AGENT'])) {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        return str_contains($user_agent, 'Mobile')
            || str_contains($user_agent, 'Android')
            || str_contains($user_agent, 'Silk/')
            || str_contains($user_agent, 'Kindle')
            || str_contains($user_agent, 'BlackBerry')
            || str_contains($user_agent, 'Opera Mini')
            || str_contains($user_agent, 'Opera Mobi');
    } else {

        return false;
    }
}

	if (is_mobileTest()) {
		echo "모바일";
	} else {
		echo "PC";
	}

 

참고 : https://gomest.tistory.com/25

Comments