Script PHP mengetahui apakah user browsing memakai Mobile Phone atau PC/Laptop

Dicatat aja deh, biar gag lupa siapa tau ada yang butuh, hehe. Ini juga hasil dari browsing sih, cuman lupa darimana aku dapet script ini. Udah lama soalnya dapetnya, baru saya posting. Mohon maaf :mrgreen:

<?php
function isMobile() {
	if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) {
		return true;
	}

	if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) {
		return true;
	}

	if(isset($_SERVER["HTTP_USER_AGENT"])){
		$user_agents = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto");
		foreach($user_agents as $user_string){
			if(preg_match("/".$user_string."/i",$_SERVER["HTTP_USER_AGENT"])) {
				return true;
			}
		}
	}

	if(preg_match("/iphone/i",$_SERVER["HTTP_USER_AGENT"])) {
		return false;
	}

	return false;
}

if (isMobile()) {
	echo "mobile";
}else{
        echo "web";
}
?>

Jika saat eksekusi file/fungsi PHP tadi yang muncul adalah mobile maka Anda sedang menggunakan browser dari Mobile Phone, jika tidak maka menggunakan PC atau sejenisnya.

Leave a Reply