Screen Resolution in PHP




6.85 K Views | Rishi Kashyap | 2 FOLLOW
    X

    Screen Resolution in PHP


    0

    I was trying to get Screen resolution using PHP server side program. Logically speaking, PHP will be parsed on the server, prior showing it to user, on the client side.

    However using simple Javascript and cookie we can send the Screen size to the Server. The logic flow is

    1. Get the screen resolution using Javascript
    2. Store the screen size data into a cookie using Javascript
    3. Read the data from cookie using PHP
    4. Use this data as you want.
    A simple PHP Script to get the Screen resolution would be <?php

    if(!isset($_COOKIE['screen_resolution'])) {
    //No cookie found so get the screen resolution using Javascript and store it in cookie
    echo '<script type="text/javascript">width = screen.width; height=screen.height; document.cookie="screen_resolution="+width+"X"+height;</script>';
    echo "Since this was the first visit, we set your Screen Resolution cookie just now.<br/> Refresh this page to see your screen resolution";}

    else {
    //Since Cookie is found so get the screen resolution from  this cookie
    $screen_size=$_COOKIE['screen_resolution'];
    echo "We got the details from Screen Resolution cookie.<br/><br/><b>Your Screen size is";
    $screen_size=explode ('X', $screen_size);
    $screen_width=$screen_size[0];
    $screen_height=$screen_size[1];
    echo "<br/>Width : $screen_width<br/>Height : $screen_height</b>";}

    ?>

    You can use $screen_width or $screen_height later as session variable and use it anywhere you want in your PHP script.

    Beware though that this method will not tell you about Window size, so if your Website visitors customize the browser size this script will not detect that.
    Check screen resolution PHP example for this PHP screen resolution in action.Screen Resolution in PHP

    Rishi Kashyap | 7 years ago | EDIT | REPLY
    • X
      0

      This is good answer ok i solve this problem simply using a jquery
      first make a screen.php file for getting screen resolution using the javascript screen.width and screen.height function and take value under session variables

      STEP 1 Read Screen width using JQUERY

      var width = screen.width;
      var height = screen.height;
      $.post("http://www.wallpapersfestival.com/ssize",{width:width,height:height},function(data){$("#width").html(data);});

      STEP 2 Read posted screen resolution
      <?php
              session_start();
      $wid=$_POST['width'];
      $heig=$_POST['height'];

      if(isset($_POST['width']) && isset($_POST['height'])) {
      $_SESSION['screen_width'] = $wid;
      $_SESSION['screen_height'] = $heig;
      $wid=$_SESSION['screen_width'];
      $heig=$_SESSION['screen_height'];
      }
      ?>   

      STEP 3 If screen resolution not set refresh the page
      <?php
      if($wid ==0)
       {
       echo '<meta http-equiv="refresh" content="2; URL="#"/>';
       }
       else {echo ""; }
      ?>

      Very easy just copy and paste the code  

      Bulbul Sharma | 3 years ago | EDIT | REPLY



    YOU MAY ALSO LIKE
    tags  JAVASCRIPT PHP TUTORIAL