PDA

View Full Version : How to detect screen resolution with PHP and Javascript?



toplisek
02-01-2011, 11:38 AM
I have set javasscript code to detect and store into variable screen resolution.
How to store this javascript into variable PHP?





<script type="text/javascript">
var ScreenWidth=screen.width; var ScreenHeight=screen.height; var ScreenResolution=ScreenWidth+"x"+ScreenHeight; document.write("<span style=\"color:red;text-transform: capitalize;\">your current screen resolution is " + ScreenResolution + "</span>");
--></script>
}
</script>

<?PHP
$screen=str_replace("#","",$resolution); echo "screen width is:".$screen;
?>

abduraooft
02-01-2011, 11:45 AM
Why is not working?
Javascript is a client side language and it executes at client's end(browser) where as PHP is a serverside one. All PHP codes will get executed before loading anything to the browser.

Krupski
02-01-2011, 03:44 PM
Javascript is a client side language and it executes at client's end(browser) where as PHP is a serverside one. All PHP codes will get executed before loading anything to the browser.

True... but there's no reason why JS can't be used to get the information then SEND it to PHP...

How to do it: LINK (http://www.wallpaperama.com/forums/how-to-get-screen-resolution-show-screen-size-script-with-php-javascript-sample-demo-t1351.html)

BTW, for the OP... I found this info with a simple Google search. Try typing "get client screen size php" in Google and see what pops up! :p

-- Roger

toplisek
02-13-2011, 07:29 PM
I have two issues:


var today = new Date();
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
var the_cookie = "users_resolution="+ screen.width +"x"+ screen.height;
var the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie=the_cookie


1. How to store more javascript variables into cookies variable?
2. How to store cookie with time stamp (Integer like PHP time stamp)
Is this cookie stored at browser till December 31, 2023?


Example script:



<script type="text/javascript">
if(navigator.javaEnabled() == true){
alert("Java is enabled");
}else{
alert("Java disabled or not installed");
}

document.write("<p>Browser: ");
document.write(navigator.appName + "</p>");

document.write("<p>Browserversion: ");
document.write(navigator.appVersion + "</p>");

</script>

Philip M
02-13-2011, 07:53 PM
The usual way to create a cookie is


function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires = "; expires = "+ date.toGMTString();
}
else{var expires = ""}
document.cookie = name + "=" + value + expires + "; path=/"; // available throughout the directory
}

createCookie("myCookieName", "value1 + '~' + value2 + '~' + value3", 365)



In other words the single cookie value is concatenated with several different values separated by (say) a ~. Then when the cookie is recovered use split("~") to extract the values again.

The days are the number of days from now before the cookie expires. Or you can set the expiry date to as specific date, as you have done.


Quizmaster: In written communication, which country's name is an acronym for "I trust and love you"?
Contestant: Germany.