Determining screen resolution

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Determining screen resolution

Post by Callan S. »

I found some code that says it will use javascript to return the resolution the user has, yet it has an odd error that I can't figure out.

Code: Select all

<?
if(!isset($_GET['r']))
{
echo "<script language=\"JavaScript\">
<!--
document.location=\"$PHP_SELF?r=1&width=\"+screen.width+\"&Height=\"+screen.height;
//-->
</script>";
}
else {   

// Code to be displayed if resolutoin is detected
     if(isset($_GET['width']) && isset($_GET['Height'])) {
               // Resolution  detected
     }
     else {
               // Resolution not detected
     }
}

?>
Yet in wamp it outputs this, for some reason echoing everything after the script ends.

Code: Select all

; } else { // Code to be displayed if resolutoin is detected if(isset($_GET['width']) && isset($_GET['Height'])) { // Resolution detected } else { // Resolution not detected } } ?>
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
kaos78414
Posts: 507
Joined: Thu Jul 22, 2010 5:36 am

Re: Determining screen resolution

Post by kaos78414 »

I found this solution which is a little cleaner, maybe it'll work for ya.

Code: Select all

<script language="javascript">
if (window.location.search == "") {
window.location.href = window.location + "?width=" + screen.width + "&height=" + screen.height;
}
</script>

Code: Select all

<?php
$width = $_get['width'];
$height = $_get['height'];

echo "You are using a $width x $height screen resolution";
?> 
w00t
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Determining screen resolution

Post by Callan S. »

Nice one, Kaos, thanks!
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Post Reply

Return to “Coding”