How can I get the resolution of a browser with PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sabin0707
Forum Newbie
Posts: 1
Joined: Fri Mar 21, 2003 6:54 am
Location: Romania
Contact:

How can I get the resolution of a browser with PHP?

Post by sabin0707 »

:?: :?: :?:
I am having some problems with getting the resolution of a browser.
I don't want to use JavaScript and this became a big problem.

Can anyone help me out?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You cannot do this with PHP. PHP is server-side, to do something like this you need to use client-side technology like Javascript.

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and here some examples

Code: Select all

<html>
	<head>
		<script type="text/javascript">
			function addResolution(oForm)
			{
				oForm.res.value = screen.width + "x" + screen.height;
				return true;
			}
		</script>
	</head>
	<body>
		<fieldset><legend>forms</legend>
			adding resolution right after "creation" of form
			<form name="form1" method="POST" action="http://localhost/test.php">
				<input type="hidden" name="res" />
				<input type="submit" name="url" value="http://where.do.you/wanna/link/today?" />
			</form>
			<script type="text/javascript">
				document.form1.res.value = screen.width + "x" + screen.height;
			</script>
			
			adding resolution when form is submitted
			<form method="POST" action="http://localhost/test.php" onSubmit="javascript:addResolution(this);" >
				<input type="hidden" name="res" />
				<input type="submit" name="url" value="http://where.do.you/wanna/link/today?" />
			</form>
			
			adding resolution while form is "created"
			<form method="POST" action="http://localhost/test.php">
				<script type="text/javascript">
					document.write('<input type="hidden" name="res" value="' + screen.width + "x" + screen.height + '" />');
				</script>
				<input type="submit" name="url" value="http://where.do.you/wanna/link/today?" />
			</form>
		</fieldset>
		
		<fieldset><legend>anchors</legend>
			adding resolution right after "creation" of anchor <br />
			<a id="a1" href="http://localhost/test.php">http://localhost/test.php</a>
			<script type="text/javascript">
				document.getElementById("a1").href += "?res=" + screen.width + "x" + screen.height;
			</script>
			<br />
			writing anchor with resolution via document.write <br />
			<script type="text/javascript">
				document.write('<a href="http://localhost/test.php?res=' + screen.width + "x" + screen.height + '">');
			</script>
				http://localhost/test.php
			</a>
		</fieldset>
	</body>
</html>
and my test.php is

Code: Select all

<html>
	<body>
	<pre><?php print_r($_REQUEST); ?></pre>
<?php
if(isset($_POST['res']))
	echo 'POSTed resolution: ', $_POST['res'];
else if(isset($_GET['res']))
	echo 'resolution via GET: ', $_GET['res'];
else
	echo 'resolution: undefined';
?>
	</body>
</html>
Post Reply