How to detect and redirect to a mobile site in PHP
Case 1 : Redirecting a site to a mobile version
Say we have a normal website that we need to redirect to a mobile version if viewed from a mobile device. All the code required for mobile detection is in a single file ‘Mobile_Detect.php’, which we can include in our web pages or in the primary index file. To simplify discussion let us assume you have a website ‘example1.com’ with a single index file, and need to redirect to ‘mobile.example1.com’ if a mobile device is detected. We need to put the following in the header of the index file of ‘example1.com’.
The above code will now redirect the main site to a mobile version if viewed from a mobile. Some other use cases to redirect are given below.
Case 2: Loading different resources depending on the device
We can load or modify existing content based on the users device profile. For example we could load a different CSS for a mobile or a tablet.
Note that mobile detection is a moving platform, and as new devices are introduced you will need to update the library with new versions. Also, as the library relies on HTTP headers to sniff the device signature, it could sometimes give incorrect results if the headers are tampered with.