PDA

View Full Version : php & javascript


angst
09-25-2006, 07:48 PM
hello,
I'm building a simple web logger for my site, and i want to log the client browser resolution, but i know that php can't provide this info.

however javascript can:


<script>
function getres(){

// get browser resolution
var browserName=navigator.appName;
if (browserName=="Netscape"){
// Netscape/FireFox
document.write(window.innerWidth+'<BR />');
document.write(window.innerHeight);
} else {
// IE
document.write(document.body.clientWidth+'<BR />');
document.write(document.body.clientHeight);
}
}
</script>
<div onclick='getres();'>get res</div>


my question is, how can i get this data into php/mysql with out using hidden form fields, which even if i did, i'm still not sure how to get the data with out something being submitted.

any ideas?

thanks in advance for your time!
-Ken

rlemon
09-25-2006, 08:09 PM
hello,
I'm building a simple web logger for my site, and i want to log the client browser resolution, but i know that php can't provide this info.

however javascript can:


<script>
function getres(){

// get browser resolution
var browserName=navigator.appName;
if (browserName=="Netscape"){
// Netscape/FireFox
document.write(window.innerWidth+'<BR />');
document.write(window.innerHeight);
} else {
// IE
document.write(document.body.clientWidth+'<BR />');
document.write(document.body.clientHeight);
}
}
</script>
<div onclick='getres();'>get res</div>


my question is, how can i get this data into php/mysql with out using hidden form fields, which even if i did, i'm still not sure how to get the data with out something being submitted.

any ideas?

thanks in advance for your time!
-Ken

the page will either have to be submitted, or using ajax you can send the data to a server page wich can store the information in a db.. maybe a session variable?..

try this.


<?
if(isset($_REQUEST['xy']))
{
session_start();
$_SESSION['xy']['x'] = split(",",$_REQUEST['xy'])[0];
$_SESSION['xy']['y'] = split(",",$_REQUEST['xy'])[1];
session_write_close();
header("location: index.php");
}
session_start();
?>
<html><head>.....
<script>
<? if(!isset($_SESSION['xy']))
{
?>
var browserName=navigator.appName;
if (browserName=="Netscape"){
// Netscape/FireFox
var x = window.innerWidth;
var y = window.innerHeight;
} else {
// IE
var x = document.body.clientWidth;
var y = document.body.clientHeight;
}
window.location = "?xy=" + x + "," + y;
<?
}
?>
</script>
</head>
<body>
x is <?=$_SESSION['xy']['x']?><br />
y is <?=$_SESSION['xy']['y']?>
</body>
</html>
<?
session_write_close();
?>

angst
09-25-2006, 08:14 PM
hmm, this looks interesting, i'll give it a shot,

thanks!
-Ken

angst
09-25-2006, 08:31 PM
nice!

it works exactly as i need it too!
good stuff!:-)

just one thing, i've updated the javascript to use:

var x = screen.width;
var y = screen.height;

this will get the full screen resolution, where as the other code would only get the browser window size. also this works in both IE and FireFox:-)

thanks again!
-Ken

rlemon
09-25-2006, 08:45 PM
nice!

it works exactly as i need it too!
good stuff!:-)

just one thing, i've updated the javascript to use:

var x = screen.width;
var y = screen.height;

this will get the full screen resolution, where as the other code would only get the browser window size. also this works in both IE and FireFox:-)

thanks again!
-Ken

not a problem.

sometimes the code isn't difficult but wrapping your mind around the logic can be.

as this is not the most effecient way (i'm sure) it should work.

angst
09-25-2006, 08:57 PM
well, really this got me going, i've cleaned it up and modified it, i think it works quite well:-) but using session to capture the info was a good idea.

and your exactly right, it's just getting the logic straight sometimes, even for simple solutions that can seem complex at first glance.