공부/Php

[PHP] PC / 모바일 환경인지 체크하기

밥성수 2020. 1. 21. 04:53

 

1. ismobile_module.php 

<?php
	class module {
		function mobileConcertCheck() {
            $mobileArray = array(
                  "iphone"
                , "lgtelecom"
                , "skt"
                , "mobile"
                , "samsung"
                , "nokia"
                , "blackberry"
                , "android"
                , "sony"
                , "phone"
            );

			$checkCount = 0;

			for($num = 0; $num < sizeof($mobileArray); $num++) {
				if(preg_match("/$mobileArray[$num]/", strtolower($_SERVER['HTTP_USER_AGENT']))) {

                                        $checkCount++;

                                        break;

                        	}

			}
			return ($checkCount >= 1) ? "mobile" : "computer";

		}

	}

?>

2. index.php

<?php include("ismobile_module.php"); ?>
<?php $obj = new module(); ?>
<?php  if($obj -> mobileConcertCheck() == "mobile") { 
	// 모바일 영역
 } else { 
 	// PC 영역 } 
?>

 

#php #모바일 #pc #모바일,PC구분하기