Javascript屏幕宽度/高度到PHP [英] Javascript screen width/height to PHP

查看:122
本文介绍了Javascript屏幕宽度/高度到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这不一定有意义,但是有没有办法在客户端上运行javascript,找到活动浏览器的屏幕宽度/高度,然后将这些变量传递给PHP脚本?

所有常识通常是PHP >> Javascript,请不要相信我认为的客户端,并且目前我找不到任何方法.

这是一个.php文件,因此很容易阻止,但我似乎无法做到这一点.

我正在创建一个JSON对象,但是后来我仍然有相同的攀岩墙.

谢谢.

作为业余爱好者,我发现技术之间的交互有些挑战,但是这让我感到很强大.也许不久之后,我将能够用手停止异常.好吧,矩阵引用失败了,因为这是人们在第二层文字级别上所做的.

解决方案

您可以执行以下操作:

<script>
document.getElementById('hidden-field').value = screen.width + ',' + screen.height;
</script>

<input id="hidden-field" name="dimensions" value="" />

并提交表格.您可以使用AJAX发送该数据,cookie,图像等.我个人将使用AJAX.

但是,是的,在这种情况下,您只需要从容面对客户即可.但是,如果您正在使用该数据做某事(例如创建图像等),则仍应验证该值是否有意义,这样您就不必最终尝试创建1亿像素宽的图像,等等.

I know it doesn't necessarily make any sense, but is there a way to run javascript on the client, find the screen width/height of the active browser, and then pass those variables to a PHP script?

All the common knowledge is usually PHP >> Javascript, never trust the client I suppose, and I can't find a way at the moment.

This is for a .php file, so blocks easily, but I cannot seem to do the reverse.

I am working on creating a JSON object, but then realized I still have the same wall to climb.

Thanks.

I am finding the interactions between technologies somewhat challenging as an amateur, but it makes me feel powerful to know. Maybe soon I'll be able to stop exceptions just with my hands. Well, the matrix reference fails, because that is what people do on a second tier literal level.

解决方案

You can do this:

<script>
document.getElementById('hidden-field').value = screen.width + ',' + screen.height;
</script>

<input id="hidden-field" name="dimensions" value="" />

and submit the form. You could use AJAX to send that data instead, a cookie, an image, etc. I personally would use AJAX.

But yes, this is a case where you'll need to just trust the client at face value. However, if you are doing something with that data (like creating an image or something) you should still validate that the value makes sense so you don't end up trying to create an image that is 100 million pixels wide, etc.

这篇关于Javascript屏幕宽度/高度到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆