在服务器端检测屏幕尺寸和像素密度?

发布于 2024-12-07 08:15:41 字数 194 浏览 4 评论 0原文

我一直在做一些研究,我想我已经知道答案了,但我想知道是否有任何方法可以在不使用 javascript 或依赖 CSS3 媒体查询的情况下获取设备的屏幕尺寸和像素密度。

本质上,我正在研究如何获取屏幕分辨率和像素密度,以便服务器可以决定在 URI 请求中服务器哪个图像。

到目前为止,我还没有发现任何东西表明这是可能的,但我想嘿,为什么不问呢?

I've been doing some research and I think I know the answer already, but I'm wondering if there's any means by which you can get a device's screen size and pixel density without the use of javascript or relying on CSS3 media queries.

Essentially, I'm looking into what it would take to get the screen resolution and pixel density so that the server could decide which image to server in a URI request.

So far I've not found anything that says this is even possible but I thought hey, why not ask?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青衫负雪 2024-12-14 08:15:41

我不完全同意上面的正确答案。实际上,这个答案在很多情况下都是正确的……但理论上却不然。通常,向 Web 服务器发出的请求包含用户代理字段,理论上,该字段可用于识别有关设备屏幕分辨率和属性的信息。

Web 请求不会首先通过客户端。它们传递到服务器,然后服务器向客户端提供页面,以便服务器首先获取请求...例如,Ruby on Rails 通过操作控制器接收对资源的请求,然后向响应提供页面。

查看 UA 解析器示例,例如: https://github.com/visionmedia/user-agent

示例用户代理我的计算机发送的信息是:

  User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like  enter code here`Gecko) Chrome/17.0.963.83 Safari/535.11

我认为通过服务器提供该信息非常有可能很好地猜测我的屏幕分辨率(DPI 等)。当然,您需要一个设备信息表来参考。

对于移动设备来说,这变得更加容易。如果用户代理是 iPad 的移动浏览器:

  Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML,   like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

您可以非常确定地知道服务器上的屏幕分辨率是多少。您甚至可以通过该信息了解浏览器的高度和宽度。我怀疑许多移动设备(例如 Android 或 Win Mo)也是如此。

总而言之,我同意这样做不切实际,但我也不同意。

我相信 Facebook 已经开展了一个主要项目,对设备和屏幕分辨率进行分类,并将其开源,因为他们在创建 Facebook 移动应用程序时遇到了类似的问题,例如,他们必须对所有移动浏览器渲染器之间的所有差异进行分类,以便他们可以根据需要定制客户端应用程序每个个案。理论上,也许该项目可能拥有执行此操作所需的信息。

I don't agree entirely with the above correct answer. Realistically this answer is correct in many cases...but theoretically it is not. Often requests made to a web server contain a User-Agent field which could, in theory, be used to discern information about device screen resolutions and properties.

Web requests do not pass through the client first. They pass to the server, which then serves a page to the client so the server gets the request first...Ruby on Rails, for example, receives a request through an action controller to a resource and then serves a page to the response.

Look at an example UA parser such as : https://github.com/visionmedia/user-agent

A sample user agent being sent by my computer is:

  User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like  enter code here`Gecko) Chrome/17.0.963.83 Safari/535.11

I think it is eminently possible to make a good guess what my screen resolution (DPI etc) is given that information via a server. You would, of course, need a table of device information to reference.

For mobile devices it gets even easier. If the User-Agent is a mobile safari for iPad:

  Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML,   like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

You can know with strong certainty what the screen resolution is on the server. You can even know the height and width of the browser with that info. I suspect the same is true with many mobile devices such as Android or Win Mo.

So in summation, I agree its impractical to do this but I also disagree.

I believe Facebook has undertaken a major project cataloging devices and screen resolutions and made it open source because they faced similar issues when creating the facebook mobile app e.g. they had to catalog all the discrepancies between all mobile browser renderers so they could tailor the client app to every individual case. Perhaps that project might have the necessary information to do this...in theory.

寄离 2024-12-14 08:15:41

Ruby 运行在服务器端——没有从客户端获取信息,它无法了解任何客户端功能。

Ruby runs on the server side--without getting info from the client, it has no way of knowing any client capabilities.

对你的占有欲 2024-12-14 08:15:41

对于不可能的事情,Mobvious 看起来做得不错:

Mobvious 检测您的应用/网站是否正在被访问
电话、平板电脑或个人电脑。然后你可以使用
这些信息贯穿您的应用程序。 (例如 fork 你的前端代码
关于设备类型。有一个 Ruby on Rails 插件
帮助你解决这个问题。)

https://github.com/jistr/mobvious

For something that's impossible it looks like Mobvious does a decent job:

Mobvious detects whether your app / website is being accessed by a
phone, or by a tablet, or by a personal computer. You can then use
this information throughout your app. (E.g. fork your front-end code
with regard to device type. There is a plugin for Ruby on Rails that
helps you with this.)

https://github.com/jistr/mobvious

轻许诺言 2024-12-14 08:15:41

我遇到了同样的问题,并使用窗口高度的 getter 和 setter 路线解决了它。如果 $height 变量为 0,则提供 get_heigt.erb,否则提供 index.erb
这是一个单用户应用程序,因此我使用一个全局变量,对于不同的用户,您必须将该信息保留在 cookie 中。
这里是重要的代码。

控制器:

get "/" do
  if $height == 0
    erb :get_height
  else
    erb :index
  end
end

get "/get_height" do
  erb :get_height
end

get "/set_height" do
  $height = params[:height]
  redirect "/"
end

get_height.erb

<script type="text/javascript">
  function send_message(message) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      document.location.reload(true);
    }
    xhttp.open("GET", "http://localhost:4567/" + message + "?height=" + window.innerHeight, true);
    xhttp.send();
  }

  send_message('set_height');
</script>

I had the same problem and solved it using a getter and a setter route for the window height. If the $height variabale is 0 the get_heigt.erb is served, otherwise the index.erb
This is a one user app, so I use a global variable, with different users you would have to keep that info in cookies.
Here the code that matters.

Controller:

get "/" do
  if $height == 0
    erb :get_height
  else
    erb :index
  end
end

get "/get_height" do
  erb :get_height
end

get "/set_height" do
  $height = params[:height]
  redirect "/"
end

get_height.erb

<script type="text/javascript">
  function send_message(message) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      document.location.reload(true);
    }
    xhttp.open("GET", "http://localhost:4567/" + message + "?height=" + window.innerHeight, true);
    xhttp.send();
  }

  send_message('set_height');
</script>
萌酱 2024-12-14 08:15:41

你可以使用 Ahoy。 current_visit 方法包含以下信息。

当有人访问您的网站时,Ahoy 会创建包含大量内容的访问
有用的信息。

流量来源 - 引荐来源网址、引荐域、登陆页面、搜索
关键字位置 - 国家、地区和城市技术 - 浏览器、操作系统、
和设备类型
utm 参数 - 来源、媒介、术语、内容、
活动

You could use Ahoy. The current_visit method contains the following information.

When someone visits your website, Ahoy creates a visit with lots of
useful information.

traffic source - referrer, referring domain, landing page, search
keyword location - country, region, and city technology - browser, OS,
and device type
utm parameters - source, medium, term, content,
campaign

桃气十足 2024-12-14 08:15:41

不确定构建有多现实,但您可以构建一个数据库,该数据库将您可以从服务器端的用户代理获取的特定设备类型映射到已知的屏幕尺寸。它仍然不允许诸如窗口大小之类的东西。

Not sure how realistic would be to build but you could build a database which maps specific device types you can get from user agent in server-side into know screen sizes. It still would not allow things like window size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文