DevelopPHP
home gear home donate
Search Results For
in:

Libraries book

HTML Library CSS Library JavaScript Library PHP Library

Videosbook

PHP and MySQL JavaScript HTML and CSS Vector and 3D Flash Actionscript Android Dev Miscellaneous

Resourcesbook

Website Hosting Developer Q+A
Sprite Programming Mastery
Mobile Device Detection and Redirection Script Using a PHP Home Page

Mobile/Handheld Device Browsers(Agents) are competing with IE, Firefox, and other popular classic browsers in a strong way. To be sure your site is ready and sized correctly you may want to detect and serve up alternate design/layouts for popular sizes and resolutions in which those devices view web pages.

You can use this PHP code on your homepage to run mobile device detection and redirection. Target any or all devices that may come into play as popular web browsing tools.

Your server must be able to process PHP for this to work for you. Server Side detection is probably the most reliable way to detect for mobile devices. There are also other methods which I will discuss in an upcoming video tutorial.

Tip - You can choose to have PHP output an alternate CSS style sheet string, instead of having multiple home pages, you can have one hompage with several different style sheets to set up the layout/sizing. Style sheet would change depending on user agent. So there are many ways to address these issues if you are concerned about mobile users viewing your pages.

Learn HTML

<?php
// Written By Adam Khoury @ developphp.com - March 26, 2010
// PHP mobile browser detection ready Home Page(index file that detects and redirects)
// Simply find the device OS or browser label and place conditionals as needed
$agent $_SERVER['HTTP_USER_AGENT']; 
// Put browser name into local variable

if (preg_match("/iPhone/"$agent)) { 
// Apple iPhone Device
    // Send them to iphone sized homepage for mobile
    
header("location: iphone_home.html"
);

} else if (
preg_match("/android/"$agent)) { 
// Google Device using Android OS
    // Send them to android sized homepage for mobile
    
header("location: android_home.html"
);
   
}
?>
<html>
<body>
<h2>This is my regular homepage for people not using the devices defined in my script.</h2>
<h3>If none of the user agents defined are detected, this page will display by default.</h3>
<strong>You are viewing this page using:</strong><br />
<em><?php echo $agent?></em>
</body>
</html>

You may need to research what popular agent names are for devices that may view your pages. I think mine are correct for iPhone and Android. But look into it yourself.

Until everything is universal we have to adapt our pages. Pretty soon we will be able to make 100% width layouts for all browsers and devices, as the resolutions for these devices grows to higher resolutions. But for now many devices view at a much smaller resolution than your typical computer monitor that is sold today in stores, so we may have to resize our websites.

Home   •   Terms of Use   •   T-Shirts   •   RSS   •   Testimonials   •   Subscribe   •   Classroom Chalkboard   •   Developer Q+A   •   Donate   •   Top ↑
Popular In PHP / MySQL E-Commerce Store Production
Social Network Website Building
Image Upload / Photo Processing
CMS Software Programming
Mass Email Systems
Magic XML Data
Search Programming
Popular In JavaScript JSON Programming
Ajax Programming
Animating Elements
WYSIWYG Programming
Date/Time Programming
DOM Scripting
Object Reference
Popular In HTML Canvas Element
New Form Elements
Audio Element
Video Element
Drag and Drop
Event Handling
Element Grouping
Popular In CSS Custom Font Embedding
Dynamic Fit Backgrounds
Theatre Mode
Box Overlays
CSS Level 1 Properties
CSS Level 2 Properties
CSS Level 3 Properties
DevelopPHP.com is a growing educational system packed with video and written material that you can access 24/7 100% free. The focus here in 2014 is spread between five technologies: HTML, CSS, JavaScript, PHP and MySQL. Learn programming theory, database interaction, web design, animation, graphics editing, vector art, 3D modeling, and much more. The educational experience here is a hybrid one offering both high quality video tutorials and code libraries that grow more robust every day. The code libraries are being assembled so that developers can discover all of the building blocks of their favorite languages. The libraries coupled with high quality video tutorials will take you deep down the rabbit hole very quickly. You can also ask questions and get free advice at WebIntersect.com.
HTML5