php 使用php获取窗口大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19291236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:11:47  来源:igfitidea点击:

Get window size with php

phpsize

提问by user2688737

What is the problem this code:

这段代码有什么问题:

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

any idea?

任何的想法?

回答by Sumit Bijvani

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

您的代码没有任何问题,但是您无法在 PHP 变量中获取任何值,因为 PHP 是服务器端脚本语言。

You need JavaScript, not PHP.

您需要 JavaScript,而不是 PHP。

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

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

然后,您可以通过 Ajax(带有XmlHttpRequest)将其发送到服务器。

回答by kba

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 和 HTML/JavaScript 如何协同工作的基本概念。PHP 是一种服务器端语言,而 JavaScript 是一种客户端语言。

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.

PHP 首先生成输出,然后将其传输到正在执行的客户端。你正试图以相反的方式做到这一点。

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 生成一个页面,将其发送到客户端以供客户端执行,然后让 JavaScript 执行调用,使用 GET/POST 请求将信息发送回 PHP。