The first problem is that there is no way in PHP to read the monitor screen size, since PHP is, after all, a server-side operation. Which means using javascript. But any javascript variables will go away once the page is downloaded, and I need that information on many pages. That means session variables - but javascript doesn't know about session variables; those are server-side.
The solution was to write the PHP code in the index.php page as a two-parter: In the first run, the program sees, in PHP, that the screen session variables do not exist, so it calls a javascript routine to get the screen size. The script then passes the data backt to index.php as URL variables; in the second pass, index.php sees these URL variables and creates session variables from them.
Because the existence of the session variiables prevents the javascript from being built into the HTML the second time, we do not have an infinite loop. The code looks like this:
In the pre-processing part of index.php:
Code: Select all
if (!isset($_SESSION)) {
session_start();
}
if (isset ($_GET['width'])) {
$_SESSION['screenWidth'] = $_GET['width'];
$_SESSION['screenHeight'] = $_GET['height'];
if (($_GET['width'] >= 1280) && ($_GET['height'] >= 1024)) {
$_SESSION['fullScreen'] = (boolean) TRUE;
} else {
$_SESSION['fullScreen'] = (boolean) FALSE;
}
}Code: Select all
<?php if (!isset ($_SESSION['screenWidth'])) { ?>
<script type="text/javascript">
width = screen.width;
height = screen.height;
if (width > 0 && height >0) {
window.location.href = "index.php?width=" + width + "&height=" + height;
} else
exit();
</script>
<?php } ?>In order to make proper use of them, I have two subroutines (these are kept in a separate .php file that gets included in any page I need it):
Code: Select all
function svWidth ($theWidth) {
if ($_SESSION['fullScreen']) return $theWidth; // full size. no adjustment needed
return round ($theWidth * ($_SESSION['screenWidth'] / 1280.0));
}
function svHeight ($theHeight) {
if ($_SESSION['fullScreen']) return $theHeight; // full size. no adjustment needed
return round ($theHeight * ($_SESSION['screenHeight'] / 1024.0));
}
1. Any page that includes these subroutines must also include this line of code FIRST:
Code: Select all
if (!isset ($_SESSION)) session_start();2. The values 1280 and 1024 are, respectively, the width and height of the screen on which I developed the web pages.
The way I use these two subroutines is mainly in the APDiv width, height, top and left parameters. First, I set up my Layout Object (using Dreamweaver) in an absolute position, and get it where I want it and how I want it to look on my development monitor (which is, remember, 1280x1024). Then, when I'm satisfied, I edit the code so it looks something like this:
Code: Select all
#apDivTitle {
position:absolute;
width:<?php echo svWidth(335); ?>px;
height:28px;
z-index:5;
left: <?php echo svWidth(810); ?>px;
top: <?php echo svHeight(300); ?>px;
Images may be similarly sized:
Code: Select all
<img src=.... width="<?php echo svWidth(544); ?>"
I learned a lot of techniques for developing my website from this and other forums, so this is my way of saying thanks, and I hope some of you will find it useful.
Dan