Get USER AGENT and modify

Asked

Viewed 33 times

0

I’m trying to set up a PHP function to check the USER AGENT

I should be able to know if you are on a mobile or desktop

After this based on this information I will apply a different style to the page with my LESS/CSS

Does anyone have any suggestions how to do?

  • 1

    Ever heard of Sinergi Browserdetector ? Maybe I can help

  • 1

    I’ll look into it, thanks friend, but I wanted to try a solution without downloading anything external

1 answer

0


Hello I use the following function to check

function verificaUserAgent() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $iphone = strpos($user_agent, "iPhone");
    $android = strpos($user_agent, "Android");
    $palmpre = strpos($user_agent, "webOS");
    $ipod = strpos($user_agent, "iPod");
    $berry = strpos($user_agent, "BlackBerry");
    $symbian = strpos($user_agent, "Symbian");
    $windowsphone = strpos($user_agent, "Windows Phone");

    if ($iphone || $android || $palmpre || $ipod || $berry || $symbian || $windowsphone == true) {
        return true;
    } else {
        return false;
    }
}

So in my HTML I do as follows to use the function return

<html class="<?= verificaUserAgent() === true ? 'mobile' : 'desktop' ?>">

In turn in your LESS or CSS file I create a main style class .mobile and another .desktop then I put all the different styles inside those classes

I hope I can help hug!

  • Our Fabio, worked perfectly, thank you so much for your solution, as vcs are fast, great day for you!

  • 1

    Glad I could help you @Maneirojr, I had the function ready use in my projects here, if my solution answered her question mark her as the right answer, a hug!

  • He wouldn’t let me accept his answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.