简体   繁体   English

将JS屏幕宽度高度保存到txt文件

[英]Save JS screen width height to an txt file

Is it possible to save on an post action request the Users screen width and height to an .txt file on my hoster? 是否可以在后处理请求中将用户屏幕的宽度和高度保存到主机上的.txt文件?

I have an post action button where redirect to an post.php file like this: 我有一个后操作按钮,其中重定向到这样的post.php文件:

<?php

header("Location: /dashboard");
$file = fopen("test.txt","a");
$ip=$_SERVER['REMOTE_ADDR'];
$hostname = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
$browser = $_SERVER["HTTP_USER_AGENT"];

foreach($_POST as $variable => $value){
    fwrite($file, $variable);
    fwrite($file, "=");
    fwrite($file, $value);
    fwrite($file, "\r\n");
}
fwrite($file,$ip."\r\n");
fwrite($file,$hostname."\r\n");
fwrite($file,$browser);

fwrite($file, "\r\n");
fclose($file);

exit;
?>

I know that i can save the screen width and height only with JS not php. 我知道我只能使用JS而不是php保存屏幕的widthheight

Theres any js code that i can add to my html file and connect it with my post.php file and save it also in my test.txt file? 我可以将任何js代码添加到我的html文件中,并将其与post.php文件连接起来,然后还将其保存在我的test.txt文件中吗?

Thanks 谢谢

You can use JS to calculate the height and Width and send it to the server using Ajax and their you can save it to txt file. 您可以使用JS计算高度和宽度,并使用Ajax将其发送到服务器,然后将其保存到txt文件中。

window.innerHeight
window.innerWidth

JS file, Add Ajax code to JS file (Include jQuery for Ajax.) JS文件,将Ajax代码添加到JS文件(包括jQuery for Ajax。)

$.ajax({
  type: "POST",
  url: "php/save.php",
  data:  {height : window.innerHeight, width: window.innerWidth}
});

Now , as your PHP already have code to save $_POST variables , there is no need to modify anything in the PHP file 现在,由于您的PHP已经具有保存$ _POST变量的代码,因此无需修改PHP文件中的任何内容

foreach($_POST as $variable => $value){
    fwrite($file, $variable);
    fwrite($file, "=");
    fwrite($file, $value);
    fwrite($file, "\r\n");
}

this code will automatically make 2 entries for height & width in file. 此代码将自动在文件中输入2个高度和宽度条目。

Using Easy way with creating 使用简单的方法进行创建

Function + Ajax. 功能+ Ajax。

Client 客户

function heightAndWidth() {

 var result = {
  height: window.innerHeight,
  width: window.innerWidth
}  

 return result

};

Ajax Request/Post Ajax请求/发布

$.ajax({type: "POST", url: "php/save.php", data: heightAndWidth() })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM