happy cat image

Start Web Coding

Your First step toward web coding,
Start Web Coding

change UI

Identifying iPhone iPad Macs

Now let's see how to distinguish between mobile and PC.
It can be accessed via $_SERVER['HTTP_USER_AGENT'], a super array of PHP.
Very little information about the device.
What value does $_SERVER['HTTP_USER_AGENT'] print?

<?php
    echo $_SERVER['HTTP_USER_AGENT'];
?>

Note that the above code is not executable in the Everbell coding exercise book. If you want to run usining EEOS.

to go to EEOS

The following is the result of executing the above code in the Chrome browser of iPhone 8 PLUS.

Here is the result of running the above code in the Chrome browser on the iPad Pro.

Here is the result of running the above code in the Chrome browser on the MacBook Pro

Looking at the image above, I see the iPhone string on the iPhone, and the iPad on the iPad.
I see a Macintosh on a MacBook. This phrase tells you which device you're using.
So you can write code like this?

<?php
    $browserAgent = $_SERVER['HTTP_USER_AGENT'];

    if(strpos($browserAgent, 'iPhone') == true) {
        echo "This device is an iPhone.";
    } else if(strpos($browserAgent, 'iPad') == true){
        echo "This device is an iPad.";
    } else if(strpos($browserAgent, 'Macintosh') == true){
        echo "This device is a Mac";
    }
?>

The following is the result of executing the above code in the Chrome browser of iPhone 8 PLUS.

Here is the result of running the above code in the Chrome browser on the iPad Pro.

Here is the result of running the above code in the Chrome browser on the MacBook.

강좌로 돌아가기