LoginSignup
0
1

More than 3 years have passed since last update.

PHPで閲覧中のデバイスの画面サイズを返す。

Last updated at Posted at 2020-05-30

下記サイトサンプルを修正、実行後リダイレクトしデバイスのwidthとheightを取得。
https://stackoverflow.com/questions/23239157/get-browser-width-using-php

get_screensize.php
<?php
session_start();
if(isset($_POST['width'])){
   $_SESSION['screen_size'] = array();
   $_SESSION['screen_size']['width'] = intval($_POST['width']);
   $_SESSION['screen_size']['height'] = intval($_POST['height']);
}


if(!isset($_SESSION['screen_size'])){
?>
<html>
<head>
<script>
function getSize(){
document.getElementById('inp_width').value=screen.width;
document.getElementById('inp_height').value=screen.height;
document.getElementById('form_size').submit();
}
</script>
</head>
<body onload='getSize()'>
<form method='post' id='form_size'>
<input type='hidden' name='width' id='inp_width'/>
<input type='hidden' name='height' id='inp_height'/>
</form>
</body>
</html>


<?php
}else{
//var_dump($_SESSION['screen_size']);
header('Location: http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/screensize_out.php');
}

?>

screensize_out.php
<?php
session_start();
echo "閲覧中のディスプレイサイズは下記の通りです!!<BR>";
   echo "width: ".$_SESSION['screen_size']['width']."<BR>";
   echo "hight: ".$_SESSION['screen_size']['height'];


?>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1