Question

I'm trying to get a little script working. It shouldn't be hard, but I'm kind of a n00b when it comes to PHP (my last experiments were many years ago).

So, basically, I want a little script that IF the client uses a mobile device, it gives a link to Facebook as an app link. IF the client has a regular laptop, the script should output a regular Facebook.

Okay, so I'm just going to show my screwed up code, to make it (hopefully) more clear what I'm trying (some might notice that I grabbed part of the code from another thread):

<?php
function check_user_agent ( $type = NULL ) {
        $user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
            if ( $type == 'mobile' ) {
                    if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
                        echo "fb://groups/334257489999204";
                    } else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
                        echo "fb://groups/123456789";
                    }
        }
        else {
            echo "https://www.facebook.com/groups/123456789";
        };
        echo "https://www.facebook.com/groups/123456789";
};
?>" />

This code is in a html anchor href tag, inside a .php file (mainly HTML though).

Thanks!

Was it helpful?

Solution

This should set you on the right path

$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( empty($user_agent) ) {
    $is_mobile = false;
} elseif ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap|phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera  mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {

          $is_mobile = true;

} else {
    $is_mobile = false;
}

  echo (is_mobile) ? 'I am a mobile !' : 'I am not a mobile :(';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top