1

Hi i know that php is server side thing... but i want know how to get user screen width through php. i search on the net, they says i have to use javascript and php together. but i don't how to make work. please tell me in simplest way to get the screen width of the user through php. if it can done through javascript only then tell me the way to pass the width variable of the javascript to the php variable in integer format(not string).

please show me codes to do it... i'm really messed up in all this... thanks :)

Pramod
  • 7
  • 1
  • 1
  • 3
  • 2
    What do you need the width for? Chances are whatever you're doing should be done on the client side anyway. – JJJ Jun 08 '13 at 05:55
  • The variable in question is `screen.width` within javascript. It's up to you how you wish to send this variable, either with a image pixel or with ajax. – Dave Chen Jun 08 '13 at 05:58
  • Don't. Just don't. You *can* set the screen width from JavaScript. So you can do an Ajax request or set a cookie from Javascript that contains the screen width, but it's unreliable, and will only work after the first page view, and only if Javascript is enabled and works. Better use [CSS media queries](http://css-tricks.com/css-media-queries/). – GolezTrol Jun 08 '13 at 05:58
  • 2
    Lots of answers already but if you say WHY you want the screen width, you will probably find help with the CSS, which is how you should usually handle screen size issues – Robert Seddon-Smith Jun 08 '13 at 06:13
  • i need screen width so that i can change the location of "add this button" based on the screen size. – Pramod Jun 10 '13 at 14:06
  • – Pramod Jun 10 '13 at 14:09
  • i can't put all this in if else because it is combination of html and javascript. what i want is that i need put these codes based on a condition like if the screen is >1024 but i can't but it because it's contains html and javascript too...... thats why thought if somehow if i can get screen width in php then may be i can put all these codes in include file and include it through php in php if else condition – Pramod Jun 10 '13 at 14:12

3 Answers3

3

Not possible with complete php you might need to use javascript too Anyway try this

<?php
session_start();
if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
    echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height'];
} else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) {
    $_SESSION['screen_width'] = $_REQUEST['width'];
    $_SESSION['screen_height'] = $_REQUEST['height'];
    header('Location: ' . $_SERVER['PHP_SELF']);
} else {
    echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
}
?>
Yogus
  • 2,143
  • 4
  • 16
  • 34
  • it's working :D thanks but will you please explain it – Pramod Jun 10 '13 at 14:22
  • Oh my goodness, this actually works! The only source on the web and stackoverflow that I could find that could put screen width and height into variables. Genius. (Might not work on IE, but what does of course ;p) – Michael d Apr 12 '16 at 15:51
1

I am not sure, But It may help you

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
    var width = $(window).width();
    //This is ajax code which will send width to your php page in the screenWidth variable
    $.ajax({
    url: "example.php", //this will be your php page 
    data : {screenwidth:width} 
    }).done(function() {
    $(this).addClass("done");
    });
    </script>

    //example.php code is here :
    <?php
    echo $width = $_REQUEST['screenwidth'];
    ?>
Orangepill
  • 23,853
  • 3
  • 38
  • 62
nana.chorage
  • 476
  • 4
  • 15
0

I agree that it is a bad idea to rely on screen width from the client but here, for your edification:

javascript:

    if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","set_screen_width.php?s="+screen.width,false);
xmlhttp.send();

If you execute this script on loading the page, it will check the screen width and send it to your php file which needs to be something like:

    <?PHP
session_start();
$_SESSION['screen']['width']=$_GET['s'];
?>

All your other PHP pages can then access the $_SESSION variable vis:

<?PHP
session_start();
$screen_width=$_SESSION['screen']['width'];

etc...

It is better not to try to send HTML that will only look good in one screen width. You can, but it's a losing game. You just don't know what the client is going to do with it. Suppose, for instance, that they have low vision and like to use a larger font.

Robert Seddon-Smith
  • 957
  • 1
  • 8
  • 13