How to send current screen resolution to PHP (on first upload)?

0

I know there are several ways to get the screen size by javascript, etc. but I have not found a satisfactory way to get this size to PHP BEFORE loading the screen.

For example, I would like the HTML / PHP file below to get the width of the screen somehow in the% PHP $largura_tela variable:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<?php echo $largura_tela; ?>

</body>
</html>
    
asked by anonymous 23.05.2018 / 20:03

1 answer

1

Look, I do not understand why you want this information in PHP, but here's an example of how it could be done ...

  

Create a file called detectScreen.php and add it to your home page:

<?php

@session_start();

if(isset($_GET['heigthJanela'])) {
    $_SESSION['screenInfo'] = [
        'heigthJanela' => $_GET['heigthJanela'],
        'widthJanela' => $_GET['widthJanela'],
        'heightTela' => $_GET['heightTela'],
        'widthTela' => $_GET['widthTela']
    ];
    echo '<script>
                window.history.go(-1);
            </script>';
    die;
} else {
    if(!isset($_SESSION['screenInfo'])) {
        echo '<script>
                window.location.href = "detectScreen.php?heigthJanela="+window.innerHeight+"&widthJanela="+window.innerWidth+"&heightTela="+screen.height+"&widthTela="+screen.width;
            </script>';
        die;
    }   
}
                                    
23.05.2018 / 20:26