1

Is it possible to save on an post action request the Users screen width and height to an .txt file on my hoster?

I have an post action button where redirect to an post.php file like this:

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

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?

Thanks

  • you can get it with JS and post it to your script. https://stackoverflow.com/questions/2242086/how-to-detect-the-screen-resolution-with-javascript – keja Jul 29 '18 at 11:02

2 Answers2

1

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.

window.innerHeight
window.innerWidth

JS file, Add Ajax code to JS file (Include 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

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.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
1

Using Easy way with creating

Function + Ajax.

Client

function heightAndWidth() {

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

 return result

};

Ajax Request/Post

$.ajax({type: "POST", url: "php/save.php", data: heightAndWidth() })
Farid Blaster
  • 974
  • 1
  • 9
  • 23