Detecting when your visitors are using mobile devices may be very useful, and sometimes you don’t need or want a complete detection plugin with advanced functionalities. So for those cases where you only need basic Mobile OS recognition, here’s a very simple PHP function you can integrate to your scripts:

 * Based on the HTTP_USER_AGENT, this function tries to detect a
 * mobile user agent and returns its name in a string.
 * @return A string that represents the mobile user agent when one
 *         is found, false otherwise.
 * @author Matt (
function getMobileAgent() {
    $mobileUA = false;

    // Make sure the user agent is set
    if(isset($_SERVER['HTTP_USER_AGENT'])) {
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);

        if(strpos($userAgent, 'android')) {
            $mobileUA = 'android';
        elseif(strpos($userAgent, 'iphone') ) {
            $mobileUA = 'iphone';
        elseif(strpos($userAgent, 'ipad')) {
            $mobileUA = 'ipad';
        elseif(strpos($userAgen, 'windows phone os 7.0')) {
            $mobileUA = 'winphone7'
        elseif(strpos($userAgent, 'blackberry')) {
            $mobileUA = 'blackberry';

    return $mobileUA;

This very simple function will tell you when a user is behind an iPhone, iPad, Android, Windows Phone 7 or Blackberry. It returns false when none of these user agents are detected. Depending on your specific needs, you way need more specific information or more advanced features. There are several open source offers out there, such as this one: Detect Mobile Browser. Also, feel free to use this code and modify it as you wish.