Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Screen res. in php?

Status
Not open for further replies.

JamesGMills

Programmer
Aug 15, 2005
157
GB
Hi,

I know you can get the screen res of a user using javascript but this causes a few problems when trying to log this info into a database on page request.

Anyone got any ideas?

------------------------
 
only javascript (or some other client side script) will give you the user's screen res.

there are lots of ways to get the info back to the server, of course.

and it's not difficult to store in a db but typically i don't bother. just store it in a session variable or work around the issues in the page design.
 
You cannot get client computer's screen resolution in PHP, because PHP runs on the server and by the time the page is served to the client, PHP is done. So you will either need to keep your javascript detection and then redirect to send this resolution to the next page via GET or POST or abandon the whole idea.
 
Yer i get the hole slient and server side thing... i thought someone might have an idea how toget around it or advice on best way?

Basicaly looks like i will have to store in session and then read it on next page request.

Or i will have to havea landing page than gathers info and then passes to a logging page and then to the requested page or some long stupid thing like that.

I want to know for stats what average screen res. is

------------------------
 
Just silently add onto the end of all your links the screenres in a GET call would be simple if amateur. eg
<script language="javascript">
document.write("<a href=mynextpage.php?sr="+screen.width+"-"+screen.height+"'>click here</a>");
</script>

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Does anyone actually read anything anyone puts on this site?

I want to record my own stats for a specific site for a specific reason.

If anyone has anything that could add which might be helpful that would be greatly appreciated if not then please just dont bother!

------------------------
 
Being helpful doesn't always mean having the answers onhand. But I did just give you a way to get the information to your PHP pages, you just have to read them in with $_GET['sr'] and stash them in your db. I have to wonder if you read my post

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Even you inyour post used the word 'amateur' way of doing it... and your 100% right... i did already suggest putting it in a session and then getting it that way which would hide it from the user.

I thank you for your reply however i am looking for a different way to do this.

Yes of course helpful does not always mean that the right answer will be at hand, thats why i posted.

------------------------
 
You're looking for a "different" way of doing this? What is it you're looking for? Here you have a method, as an alternative to the session method which you wanted an alternative too, but still it's not what you're looking for? Perhaps you'd like to detail more of what you're looking for. I fail to see why giving your php pages access to their screenres in either of those ways stops you from entering it into your DB and achieving your goal. It doesn't involve lots of pages either.
Of course you could have an iframe somewhere on the page that autorefreshes and sends the information to you, or use something like flash though even that needs clicking to activate now afaia.

Really - there is no way to get the information to you any earlier than when clientside JS runs and sends information back to the server, yet you're saying this isn't what you want. There simply is no way to have on your server information about their machine without the machine sending it back which involves GET or POST and you coding the handling.

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
here is a suggestion that is transparent to the user. and imho decidedly not amateur.

create a php page called stats.php. code as follows
Code:
<?
echo stats();
function stats() {
	if (session_id() == "") session_start();
	if (isset($_POST['height']))
		$_SESSION['height'] = $_POST['height'];
	if (isset($_POST['width']))
		$_SESSION['width'] = $_POST['width'];
	
	if (!empty($_SESSION['width']) && !empty($_SESSION['height']) ):
		return "entries updated to session : " . session_id();
	else:
		return "error.  posted data was " . print_r($_POST, true);
	endif;
}
?>
next create a js page called stats.js with the following code
Code:
function initObject(){
	var A;
	var msxmlhttp = new Array(
				'Msxml2.XMLHTTP.5.0',
				'Msxml2.XMLHTTP.4.0',
				'Msxml2.XMLHTTP.3.0',
				'Msxml2.XMLHTTP',
				'Microsoft.XMLHTTP');
			for (var i = 0; i < msxmlhttp.length; i++) {
				try {
					A = new ActiveXObject(msxmlhttp[i]);
				} catch (e) {
					A = null;
				}
			}
 			
			if(!A && typeof XMLHttpRequest != "undefined")
				A = new XMLHttpRequest();
			if (!A)
				A = false;
			return A;
}

window.onload = function ()
{
    var objHTTP, w, h;
	w = screen.width;
	h = screen.height;
	var statspage = "statspage.php";
	objHTTP = initObject();
        if (!objHTTP) { return false; }
	var postdata = "height="+h+"&width="+w;	

    objHTTP.open ('POST',statspage,false);
	objHTTP.setRequestHeader ("Method", "POST " + "statspage" + " HTTP/1.1");
    objHTTP.setRequestHeader('Content-Type','application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
    objHTTP.send(postdata);
	alert  (objHTTP.responseText);
}

and finally in your html include the js file. for example
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Screen res test</title>
<script src="stats.js" language="javascript"> </script>
</head>
<body>
Content goes here
</body>
</html>

obviously you can alter the code in the php file to do whatever you want with the height and width data. and you can suppress the alert by deleting the line in the js file. finally if you are concerned about the availability of the server with the stats.php file on it you could change the "false" in the objHTTP.open line to true (to force the code to run asynchronously).

the stats page is complex in order to be cross-browser compliant (so far as practical). (ps. bits of it are borrowed from modernmethod.com)

obviously you can send whatever information you like about the browser back up the postdata stream.

another alternative would be to set a domain cookie in the page which the browser would send back in the next page request. Marginally less code overhead but you risk not getting a page refresh.


i can't think of any other "less amateur" ways than this.
 
Thanks for such a nice reply, and thanks for taking the time to go through all that.

I am going to take some of what youhave suggested and play with it a bit.

Thanks again

PS Leozack, you need to chill out, i was not having a go at your or anyone really, just this forum sometimes gets to me because people should just not post a reply if they can not help or dont have anything good to say.

------------------------
 
Perhaps - but I don't thinka nyone's replies were unhelpful or wihtout something good to say.

1 thing I notie a lot about posting here is unlike many places you're likely to get a response quickly no matter what day or time. And when you do, it may not be "here's the solution you want and I even giftwrapped it for you". Often it's other people like you who don't have the answer but who are willing to help so they'll post replies adn try out code and work WITH you to try and solve it. This is, imo, a lot better than not replying just because they don't have the answer. I'd rather have a community of people willing to help and get there in the end than a hit or miss one where noone replies unless they have the answer and noone is willing to work through it with you until it gets solved. I have threads where it took weeks to find the answer and many attempts. It's all good.

I've rarely seen things/people put down on here, and when it is it seems to be by some form of mangement position who want the impossible as usual >_>

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Might I suggest giving jpadie a star for their work assuming it's helped you though noone helps on the premise of getting them.

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top