Get window size with php [closed]

2019-06-14 19:30发布

问题:

What is the problem this code:

$window_width= '<script>screen.availWidth</script>';
$window_height= '<script>screen.availHeight</script>';

any idea?

回答1:

Nothing wrong in your code, but you can't get any values in PHP variable, because PHP is Server side scripting language.

You need JavaScript, not PHP.

var screenWidth = window.screen.width,
var screenHeight = window.screen.height;

You can then send it to the server via Ajax (with an XmlHttpRequest).



回答2:

You seem to be misunderstanding the basic concepts of how PHP and HTML/JavaScript works together. PHP is a server-side language and JavaScript is a client-side language.

PHP generates the output first and then it's transferred to the client where it's being executed. You're trying to do it the other way around.

What you need to do is first, generate a page using PHP, have it sent to the client for client-side execution, and from there have JavaScript perform a call that sends the information back to PHP using a GET/POST request.



标签: php size