After detecting if my web site is used on a mobile device of not by parsing the USER_AGENT string, I would like to redirect the user to the pages optimized for resolutions with height >= 1024 (for example 9.7", 10.1" tablets) or redirect the user to pages optimized for resolutions < 1024 (for example smarphones and 5", 7", 8" tablets)
To acheive this I need to get the screen resolution with javascript.
We know that PHP (the USER_AGENT parser script) is executed before javascript but I need to get the screen resolution before the parser. As I know that a web server executes firstly INDEX.HTML and then INDEX.PHP I wrote a Index.html file which gets the screen width and pass it via a form with POST (to keep the address bar clean rather than $_GET) to Index.php which then loads (includes) the corrects web page.
INDEX.HTML
<body onload="document.form.submit();">
<form name="form" action="index.php" method="POST">
<input type="hidden" id="height" name="height" value="" />
</form>
<script type="text/javascript">
document.getElementById('height').value = screen.height;
</script>
</body>
INDEX.PHP
<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];
// If it's a mobile device...
if(preg_match(...) {
// If the device screen height is => 1024 it's a tablet 8", 9.7" or 10.1"
if ($_POST['height'] >= 1024) {
include ('include.tablet.php');
}
// If the device screen height is < 1024 it's a smartphone or a tablet 5" o 7")...
else {
include ('include.smartphone.php');
}
}
// If it's a desktop PC...
else {
include ('include.index.php');
}
}
?>
Now I'm trying to do the same thing without the HTML file. I wrote a index.php file which gets screen.width and send it to itself with $_SERVER['PHP_SELF'] so that the PHP code may redirect the user to the correct page.
<body onload="document.form.submit();">
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" id="height" name="height" value="" />
</form>
<script type="text/javascript">
document.getElementById('height').value = screen.height;
</script>
<?php if(isset($_POST['height'])) {
// conditional include
} ?>
</body>
The issue is that the method works, but the browser (Firefox) take 1 or 2 seconds to run the page and return the result. Why?
NO JAVASCRIPT, NO JQUERY: I know that I could use a javascript parser and then redirect to the correct page with: 'window.location.href' but I want my homepage to be always 'index.php' and not start with index.php which redirects to tablet.php or smartphone.php. I'm trying to keep the code elegant.
Any suggestion?