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

Asked

Viewed 1,967 times

1

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

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

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

<?php echo $largura_tela; ?>

</body>
</html>
  • 1

    You can’t communicate this before because PHP runs before Javascript. It will take at least 2 requests to get this information to the server.

  • @bfavaretto You’re right. Could post your comment as reply to I give as accepted?

  • @Leo Caracciolo, both jquery and javascript run on the client side, the question is how to make PHP have this information on the first load, which is impossible, only if you do a redirect, hence it is already the second load.

  • @Leo Caracciolo, I don’t think you understand yet, the question is how PHP can have this data in the first load, not jquery....

  • Boy, how long do you live? That’s not possible with PHP and everyone is bald to know. I was just providing you an alternative that does not refresh the page. Good luck there for you.

  • @Leo Caracciolo, sorry, it is that I thought you had not understood, but I appreciate your effort and your attention. Thank you.

  • Relax!! It was nice that I developed another script!

Show 2 more comments

2 answers

1

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

Create a file called detectScreen.php and include it on your main 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;
    }   
}

After this you can recover in your codes using the example below (test.php):

<?php include __DIR__ . '/detectScreen.php'; ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<?php echo $_SESSION['screenInfo']['heightTela']; ?>

</body>
</html>
  • It doesn’t really work. The ideal way to leave an answer is to test it. And from what I see, there is no way to send the screen resolution to php on the first load...

  • I updated the answer! Now it is tested, in the first load has no way even, but has the above alternative.

-1

<?php
if(!isset($_GET['r']))
{
echo "<script language=\"JavaScript\">
<!--
document.location=\"$PHP_SELF?r=1&Largeur=\"+screen.width+\"&Hauteur=\"+screen.height;
//-->
</script>";
}
else {

// Código para exibir em caso de detecção da resolução de exibição
if(isset($_GET['Largeur']) && isset($_GET['Hauteur'])) {
  $largura = $_GET['Largeur'];
  $altura = $_GET['Hauteur'];
}
else {
// $largura = "nao encontrada";
}
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.