Get window size with php [closed]

2019-06-14 18:59发布

What is the problem this code:

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

any idea?

标签: php size
2条回答
我想做一个坏孩纸
2楼-- · 2019-06-14 19:40

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.

查看更多
女痞
3楼-- · 2019-06-14 19:51

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).

查看更多
登录 后发表回答