php get screen width

Solutions on MaxInterview for php get screen width by the best coders in the world

showing results for - "php get screen width"
Erika
29 May 2019
1//Save in cookie to access with $_COOKIE["screenXYZ"];
2
3//SetScreenResToCookie.php
4<?php
5if (isset($_POST)) {
6    echo "success";
7    setcookie("screenW", $_POST["screenWidth"], time() + (86400 * 30), '/');
8    setcookie("screenH", $_POST["screenHeight"], time() + (86400 * 30), '/');
9} else {
10    echo "Error. $" . "_POST array is not set!";
11}
12?>
13  
14//where u need the Screen res(only a example):
15  
16//header.php
17//The function 
18<?php
19function screenResToCookie()
20{
21 if (isset($_SERVER["HTTP_X_FORWARDED_PROTO"])) {
22        $http = $_SERVER["HTTP_X_FORWARDED_PROTO"];
23    } else {
24        $http = $_SERVER["REQUEST_SCHEME"];
25    }
26    $url = $http . "://" . $_SERVER["HTTP_HOST"];
27    echo "<script>
28        var damn = setInterval(() => {
29            var formData = {
30                screenWidth: screen.width,
31                screenHeight: screen.height
32            };
33
34            $.ajax({
35                url: '" . $url ."/.../SetJScookie.php?" . time() ."',//This needs to be the URL on the domain u access the website from. ajax dont allow HTTPS
36                type: 'POST',//because 'SetScreenResToCookie.php' use POST Array
37                data: formData, //data in json format
38                async: false, //enable or disable async (optional, but suggested as false if you need to populate data afterwards)
39                success: function(response, textStatus, jqXHR) {
40                    console.log(response);
41                },
42                error: function(jqXHR, textStatus, errorThrown) {
43                    console.log(jqXHR);
44                    console.log(textStatus);
45                    console.log(errorThrown);
46                }
47            });
48        }, 1000);
49    </script>";
50}
51
52function ReturnHeader($..., $...) {
53  $Mobile = false;//If we dont wanne use Bootstrap or @media
54  screenResToCookie();//set the cookie for us
55  if (intval($_COOKIE["screenW"]) < 735) {
56        $Mobile = true;
57    }
58  
59  if ($Mobile) {
60	//print header with mobile style(Dropdowns added or whatever)
61  } else {
62   //print header with Computer/Tablet style 
63  }
64}
65?>
66
67
Isabelle
02 Mar 2016
1<button onclick="myFunction()">Try it</button>
2
3<p id="demo"></p>
4
5<script>
6function myFunction() {
7  var x = "Total Width: " + screen.width + "px";
8  document.getElementById("demo").innerHTML = x;
9}
10</script>