Getting Screen Resolution PHP

// jQUERY FILE

$(function() {
    $.post('some_script.php', { width: screen.width, height:screen.height }, function(json) {
        if(json.outcome == 'success') {
            // do something with the knowledge possibly?
        } else {
            alert('Unable to let PHP know what the screen resolution is!');
        }
    },'json');
});

// PHP FILE

<?php
// For instance, you can do something like this:
if(isset($_POST['width']) && isset($_POST['height'])) {
    $_SESSION['screen_width'] = $_POST['width'];
    $_SESSION['screen_height'] = $_POST['height'];
    echo json_encode(array('outcome'=>'success'));
} else {
    echo json_encode(array('outcome'=>'error','error'=>"Couldn't save dimension info"));
}
?>

3.5
2
Keyboard-k 100 points

                                    //Save in cookie to access with $_COOKIE[&quot;screenXYZ&quot;];

//SetScreenResToCookie.php
&lt;?php
if (isset($_POST)) {
    echo &quot;success&quot;;
    setcookie(&quot;screenW&quot;, $_POST[&quot;screenWidth&quot;], time() + (86400 * 30), '/');
    setcookie(&quot;screenH&quot;, $_POST[&quot;screenHeight&quot;], time() + (86400 * 30), '/');
} else {
    echo &quot;Error. $&quot; . &quot;_POST array is not set!&quot;;
}
?&gt;
  
//where u need the Screen res(only a example):
  
//header.php
//The function 
&lt;?php
function screenResToCookie()
{
 if (isset($_SERVER[&quot;HTTP_X_FORWARDED_PROTO&quot;])) {
        $http = $_SERVER[&quot;HTTP_X_FORWARDED_PROTO&quot;];
    } else {
        $http = $_SERVER[&quot;REQUEST_SCHEME&quot;];
    }
    $url = $http . &quot;://&quot; . $_SERVER[&quot;HTTP_HOST&quot;];
    echo &quot;&lt;script&gt;
        var damn = setInterval(() =&gt; {
            var formData = {
                screenWidth: screen.width,
                screenHeight: screen.height
            };

            $.ajax({
                url: '&quot; . $url .&quot;/.../SetJScookie.php?&quot; . time() .&quot;',//This needs to be the URL on the domain u access the website from. ajax dont allow HTTPS
                type: 'POST',//because 'SetScreenResToCookie.php' use POST Array
                data: formData, //data in json format
                async: false, //enable or disable async (optional, but suggested as false if you need to populate data afterwards)
                success: function(response, textStatus, jqXHR) {
                    console.log(response);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(jqXHR);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        }, 1000);
    &lt;/script&gt;&quot;;
}

function ReturnHeader($..., $...) {
  $Mobile = false;//If we dont wanne use Bootstrap or @media
  screenResToCookie();//set the cookie for us
  if (intval($_COOKIE[&quot;screenW&quot;]) &lt; 735) {
        $Mobile = true;
    }
  
  if ($Mobile) {
	//print header with mobile style(Dropdowns added or whatever)
  } else {
   //print header with Computer/Tablet style 
  }
}
?&gt;

3.5 (2 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source