Techniqued- Technoloy, Review and Tutorials
  • Android
  • Freewares
  • Tutorial
  • Windows 11
  • macOS
No Result
View All Result
Techniqued- Technoloy, Review and Tutorials
  • Android
  • Freewares
  • Tutorial
  • Windows 11
  • macOS
No Result
View All Result
Techniqued- Technoloy, Review and Tutorials
No Result
View All Result

[How To] Detect a Mobile or Tablet Devices in PHP Code

Nirmal by Nirmal
April 13, 2013
in Tips and Tricks
5
Share on FacebookShare on Twitter

Today blog and website designs have one important aspect, responsive layout. Responsive design is where a website layout adjusts itself to the device from which it is being viewed. So as a webmaster, it is tough job to add elements like advertisement or other codes to a responsive design as it might just break up. For example, you want to show a particular advertisement only when website is viewed on desktop and rest all cases you want to skip the ad. In such cases how do we detect the device from which is being viewed and hide the code?

In such cases, Mobile_Detect will be of great help. It is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Mobile Detect

Using the code, you can detect all the popular mobile and tablet browsers. You can even write separate code for iOS, Android etc which means these code will be executed based on the condition. The script is as reliable as server-side detection can be. This is not a replacement for RWD (media queries) or other forms of client-side detection.

You can also check for certain phones, like isiPhone() fetches whether the website is opened from iPhone. There are quite a lot of options available and you can view the complete list here.

How to Use this Code:

The usage of the code is pretty simple, download the Mobile_Detect class file from here and upload it your theme directory. Now you can simply call the class anywhere in the php code.

<?php
include 'Mobile_Detect.php';
$detect = new Mobile_Detect;

// 1. Check for mobile environment.

if ($detect->isMobile()) {
    // Your code here.
}

// 2. Check for tablet device.

if($detect->isTablet()){
    // Your code here.
}

// 3. Check for any mobile device, excluding tablets.

if ($detect->isMobile() && !$detect->isTablet()) {
    // Your code here.
}

Here the code gets the device and then executes the If condition based on it. You can find typical use cases here.

If you are using WordPress blog, you can check out the WP Mobile Detect plugin which uses this class, which means you do not have to manually write the code.

The PHP class is really helpful in executing the code based on the device on which it is being opened. We tested it on a WordPress site and it worked perfectly.

You can download Mobile Detect from here.

Tags: Detect Mobile DevicesHow toMobile DetectPHP CodeTipsTricks
ShareTweetSendSend
Previous Post

Pingdom’s Real User Monitoring

Next Post

[How To] Unroot Any Android Phone or Tablet

Related Posts

Tips and Tricks

Can I Run Windows 11 without Activating

June 14, 2024
Social Networks

How to Change your Instagram Name

May 12, 2024
Social Networks

How To View Your Instagram Reels Watch History

May 11, 2024
Social Networks

How To Post Longer Videos On Instagram

May 6, 2024
Social Networks

How to Find Who Unfollowed you on Instagram

December 9, 2023
Social Networks

How to Download Instagram Reels [Official Method]

November 28, 2023

Comments 5

  1. Kamal Nayan says:
    12 years ago

    Is it better than what we use to identify screen sizes in CSS? Just wanted your consent over this.

    Reply
    • Nirmal T V says:
      12 years ago

      You can identify screen size using CSS, but you cannot execute conditions based on it, I meant scripts.

      Reply
    • Serban Ghita says:
      12 years ago

      Use CSS media queries for document layout and content adaptation but use Server-Side detection for serving less or different JavaScript/CSS/HTML content for mobile devices.

      Reply
  2. Kamal Nayan says:
    12 years ago

    Is it better than what we use to identify screen sizes in CSS? Just wanted your consent over this.

    Reply
  3. Kamal Nayan says:
    12 years ago

    Is it better than what we use to identify screen sizes in CSS? Just wanted your consent over this.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.



Latest Articles

How to Fix “Installation can’t continue” when trying to Update Nvidia Graphics Drivers

Download Windows 11 24H2 ISO (Offline Installer)

Can I Run Windows 11 without Activating

How to Use Snapchat on PC

How to Delete YouTube Search and Watch History on iPhone

How to Use Energy Saver on Windows 11

Techniqued- Technoloy, Review and Tutorials

© 2021 Techniqued - Technology, Tips, Tricks and Reviews

Navigate Site

  • About Us
  • Advertise with us
  • Contact Us
  • Copyright Policy
  • Disclaimer

Follow Us

No Result
View All Result
  • Android
  • Freewares
  • Tutorial
  • Windows 11
  • macOS

© 2021 Techniqued - Technology, Tips, Tricks and Reviews

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.