PHP Classes

Mobile Detection: Detect mobile devices from request headers

Recommend this page to a friend!
  Info   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 52%Total: 1,161 This week: 3All time: 3,263 This week: 42Up
Version License PHP version Categories
mobile-detection 1.3.1MIT/X Consortium ...5.0HTTP, PHP 5, Wireless and Mobile
Description 

Author

This class can detect mobile devices from request headers.

It can analyze the HTTP request user agent and accept headers to determine whether the user accessing the server is using a known mobile device.

The class can also redirect the request to a separate domain or sub-directory of the current domain if the user is using a mobile device.

It also supports special GET request parameters that when present will force the class treat the current request as coming from a mobile device or not.

Picture of Harry Kämpf
  Performance   Level  
Name: Harry Kämpf <contact>
Classes: 1 package by
Country: Germany Germany
Age: ???
All time rank: 2167135 in Germany Germany
Week rank: 51 Up4 in Germany Germany Up

Details

clsMobileDetection.php ====================== class to detect mobile devices based on the user agent and redirect on different ways Usage: include this class as first line, before you send any content to the browser, e.g. require_once('clsMobileDetection.php'); (see index.php) activate in clsMobileDetection.php one of the last lines $objMobile->RedirectMobile("SUBDOM"); if you want to redirect to a subdomain, e.g. http://m.my-pages.com/... or $objMobile->RedirectMobile("MOBIDOM"); if you want to redirect to a .mobi domain, e.g. http://www.my-pages.mobi/... or $objMobile->RedirectMobile("SUBDIR"); if you want to redirect to a special subdirectory, e.g. http://www.my-pages.com/m/... alternative: if you prefer to switch between different script sequences inside the same script, use the IsMobile()-method (see index2.php) additional: if you use the get-parameter noMobile, e.g. http://www.my-pages.com?noMobile=true => the redirection will be bypassed; This is very useful, if your are inside the mobile version of a webpage and you will offer an alternate link to the conventional webpage. The decision will be saved in a cookie for the next visits. If you want to go back to the mobile version (independs of the agent), use the get-parameter Mobile, e.g. http://www.my-pages.com?Mobile=true => The agent-detection will be bypassed and the cookie will be deleted. => I advice to add the canonical link tag on each page (e.g. <link rel="canonical" href="http://www.my-pages.com/page.php" />) in order to prevent duplicate content (because call with/without noMobile/Mobile parameter) ===================================================================================================================================== Klasse zur Erkennung von mobilen Geräten auf Basis des User-Agents und Weiterleitung zu der mobilen Version der WebSite Verwendung: diese Klasse als erste Zeile einfügen - noch bevor Inhalte an den Browser gesendet werden, z.B. require_once('clsMobileDetection.php'); (siehe index.php) in clsMobileDetection.php Aktivierung einer der letzten Zeilen $objMobile->RedirectMobile("SUBDOM"); für die Umleitung zu einer Subdomäne, z.B. http://m.my-pages.com/... oder $objMobile->RedirectMobile("MOBIDOM"); für die Umleitung zu einer .mobi Domäne, z.B. http://www.my-pages.mobi/... oder $objMobile->RedirectMobile("SUBDIR"); für die Umleitung in ein spezielles Unterverzeichnis, z.B. http://www.my-pages.com/m/... Alternative: Wenn die Unterscheidung innerhalb der gleichen Webseite erfolgen soll, z.B. nur Verwendung einer unterschiedlichen DOCTYPE oder unterschiedlicher Stylesheets, dann kann man die Methode IsMobile() verwenden (siehe index2.php) Zusatz: Bei Verwendung des Get-Parameters noMobile, z.B. http://www.my-pages.com?noMobile=true wird kein automatisches Redirect ausgeführt. Man bleibt auf der konventionellen Seite stehen. Dies ist nützlich, wenn man sich z.B. auf der mobilen Version einer Webseite befindet und einen alternativen Link zu der herkömmlichen Version einer Webseite anbieten will. Die Auswahl wird in einem Cookie für die nächsten Besuche gespeichert. Will man die mobile Version anzeigen, unabhängig vom Agent, dann kann man den get-parameter Mobile verwenden, z.B. http://www.my-pages.com?Mobile=true => Die Erkennung anhand des UserAgents wird übergangen und es wird zu der mobilen Version weiter geleitet. In diesem Fall wird ein evtl. vorh. Cookie gelöscht. Empfehlung: Bitte das canonical link tag auf jeder Seite angeben (z.B. <link rel="canonical" href="http://www.my-pages.com/page.php" />), um duplicate content vorzubeugen (wegen des möglichen Aufrufes mit und ohne noMobile/Mobile Parameter) ===================================================================================================================================== author Harry Kämpf <kontakt@kaempf-nk.de> version 1.3.1 - 2015-01-10 license http://www.opensource.org/licenses/mit-license.php ===================================================================================================================================== history: version 1.3.1 - 2015-01-10 replace the user-agent pattern array by regular expressions revision of user-agent pattern (more detailed) version 1.3 - 2012-04-05 get-parameter mobile=true (new) cookie (new) version 1.2 - 2012-01-12 bug fixing version 1.1 - 2012-01-10 https-Support (new) version 1.0 - 2011-12-28 first release

  Files folder image Files  
File Role Description
Plain text file clsMobileDetection.php Class mobile detection class
Accessible without login Plain text file readme.txt Doc. Documentation / Explanation
Accessible without login Plain text file index.php Example example for redirect to subdomain, .mobi domain or subdirectory
Accessible without login Plain text file index2.php Example example for switch content in the same page

 Version Control Unique User Downloads Download Rankings  
 0%
Total:1,161
This week:3
All time:3,263
This week:42Up
 User Ratings  
 
 All time
Utility:68%StarStarStarStar
Consistency:62%StarStarStarStar
Documentation:62%StarStarStarStar
Examples:56%StarStarStar
Tests:-
Videos:-
Overall:52%StarStarStar
Rank:2329