12 Jun 2008 @ 5:27 AM 
 

Finding screen size inside of PHP

 

So I'm working on a side project that's mainly just for me, but it's so damn handy I might just make it publicly available when it's done (probably for a small monthly fee). It's basically a super quick way to run certain types of scan for stocks during the day, picking out data that's usually hard to get until the end of the day. I'm tying it into the OptionsXpress API so that when I identify a good entry or exit point on a stock, I can change my position with the click of a button.

I've been designing the site to automatically rescale for my mobile phone, but I need a way to determine if I'm looking at it on the phone or on a desktop - and since my mobile browser is generally configured to identify as a desktop browser (so that I don't get sent to the "mobile version" of the sites I visit) it's harder to do. My browser does a great job of rendering full-screen pages and scaling them - if you've seen the iPhone browser, both Opera Mini and Pixsel do something similar though without 'multi-touch'.

That said, there's something to be said for designing a website that will gracefully scale when it's on a phone.

But when the browser identifies itself as a desktop browser, what's left? Javascript can pick up the screen size, but what about PHP? All my pages are created in PHP...

Well, first create and save this file in the directory with the file you need screen size data in PHP: (You should call this file "screensize.js")

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
 
var screenW = 640, screenH = 480;
if (parseInt(navigator.appVersion)>3) {
 screenW = screen.width;
 screenH = screen.height;
}
else if (navigator.appName == "Netscape"
    && parseInt(navigator.appVersion)==3
    && navigator.javaEnabled()
   )
{
 var jToolkit = java.awt.Toolkit.getDefaultToolkit();
 var jScreenSize = jToolkit.getScreenSize();
 screenW = jScreenSize.width;
 screenH = jScreenSize.height;
}
 
	readWCookie = readCookie("screenW");
	if (readWCookie < 10)
	{
	document.cookie =
		'screenW='+screenW+'; path=/';
 
	document.cookie =
		'screenH='+screenH+'; path=/';
	location.reload(true);
	}

Then, your PHP file will require something like this in the header:

<script src="screensize.js" type="text/javascript"></script>

And something like this in the PHP section of the body:

$userAgent = $_SERVER['HTTP_USER_AGENT'];
$screenW = $_COOKIE['screenW'];
$screenH = $_COOKIE['screenH'];
print "Width:$screenW Height:$screenH";

Tags Tags: , , , , , , , , , , , , , , , , , , ,
Categories: Uncategorized, geektalk
Posted By: Eddie
E-mail | Permalink |
 

Responses to this post » (None)

 


Comments are open. Feel free to leave a comment below.


 Comment Meta:
RSS Feed for comments
TrackBack URI
 

Leave A Comment ...

 

 XHTML:
You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
\/ More Options ...

Fatal error: Call to undefined function get_avatar() in /home/opaqoff5/public_html/shakeartistcom/blog/wp-content/themes/inanis-glass/footer.php on line 79