php get screen size

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

showing results for - "php get screen size"
Aitana
08 Mar 2019
1// jQUERY FILE
2
3$(function() {
4    $.post('some_script.php', { width: screen.width, height:screen.height }, function(json) {
5        if(json.outcome == 'success') {
6            // do something with the knowledge possibly?
7        } else {
8            alert('Unable to let PHP know what the screen resolution is!');
9        }
10    },'json');
11});
12
13// PHP FILE
14
15<?php
16// For instance, you can do something like this:
17if(isset($_POST['width']) && isset($_POST['height'])) {
18    $_SESSION['screen_width'] = $_POST['width'];
19    $_SESSION['screen_height'] = $_POST['height'];
20    echo json_encode(array('outcome'=>'success'));
21} else {
22    echo json_encode(array('outcome'=>'error','error'=>"Couldn't save dimension info"));
23}
24?>
Sebastián
06 Nov 2020
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
Melia
04 Jul 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
Dana
06 Jun 2017
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>