Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Hour 17. Detecting Mobile Devices > Playing Device Detective with PHP

Playing Device Detective with PHP

You have seen how you can use an .htacess file to rewrite the requests coming from mobile browsers to direct them to a mobile section or page of your site. While using that method works, more developers seem to have a deeper knowledge of using PHP than they have of working with .htaccess files. PHP is an excellent language to start with because it is readily available on most web host providers, and it runs with most servers that you can download and use on your own system.

While it is impossible for me to cover everything that you can do with PHP in a couple of pages, I can show you how to detect the UA with PHP. To get started let’s take a look at a built-in PHP variable named $_SERVER.

The $_SERVER variable is actually an array and contains many elements that you can use to find out information about the server itself as well as some information about the client visiting it. It is important to note that when using the $_SERVER variable, it may not always return values. This is because some servers are configured not to pass some data to the PHP variable. Listing 17.2 shows the content of detect.php, a simple PHP file that is being used to display some information using the $_SERVER variable.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint