Asked  7 Months ago    Answers:  5   Viewed   41 times

I need to find the screen resolution of a users screen who visits my website?

 Answers

21

You can't do it with pure PHP. You must do it with JavaScript. There are several articles written on how to do this.

Essentially, you can set a cookie or you can even do some Ajax to send the info to a PHP script. If you use jQuery, you can do it something like this:

jquery:

$(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 (some_script.php)

<?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"));
}
?>

All that is really basic but it should get you somewhere. Normally screen resolution is not what you really want though. You may be more interested in the size of the actual browser's view port since that is actually where the page is rendered...

Wednesday, March 31, 2021
 
erotsppa
answered 7 Months ago
99

You bind to a host that most likely does not resolve to 127.0.0.1 but connect to 127.0.0.1. That cannot work. You have two options:

  1. Use 127.0.0.1 (or localhost) on both sides
  2. Use the hostname VAC01.VACLab.com on both sides
Saturday, May 29, 2021
 
commonpike
answered 5 Months ago
73

You can get info on the display from the DisplayMetrics struct:

DisplayMetrics metrics = getResources().getDisplayMetrics();

Though Android doesn't use a direct pixel mapping, it uses a handful of quantized Density Independent Pixel values then scales to the actual screen size. So the metrics.densityDpi property will be one of the DENSITY_xxx constants (120, 160, 213, 240, 320, 480 or 640 dpi).

If you need the actual lcd pixel density (perhaps for an OpenGL app) you can get it from the metrics.xdpi and metrics.ydpi properties for horizontal and vertical density respectively.

If you are targeting API Levels earlier than 4. The metrics.density property is a floating point scaling factor from the reference density (160dpi). The same value now provided by metrics.densityDpi can be calculated

int densityDpi = (int)(metrics.density * 160f);
Tuesday, June 1, 2021
 
Silfverstrom
answered 5 Months ago
87

why dont you keep it simple and clean

<?php
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    $acceptLang = ['fr', 'it', 'en']; 
    $lang = in_array($lang, $acceptLang) ? $lang : 'en';
    require_once "index_{$lang}.php"; 

?>
Tuesday, June 1, 2021
 
mgraph
answered 5 Months ago
94

In the SDL 2 wiki, you have a category named Display and Window Management. It lists everything you need to know about SDL 2's management of displays (screens) and windows.

You have multiple choices, the most generic would be using SDL_GetCurrentDisplayMode or SDL_GetDesktopDisplayMode. The difference is explained in the wiki :

There's a difference between SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() when SDL runs fullscreen and has changed the resolution. In that case SDL_GetDesktopDisplayMode() will return the previous native display mode, and not the current display mode.

After setting a SDL_DisplayMode with one of these, you can retrieve its attributes w and h.

However, there is another function that might be more appropriate and straightforward : SDL_GetDisplayBounds. If I am not mistaken, it gives you the coordinates of the display relative to the whole set of displays that can be active on the computer, and also the size of the display.

Both methods need you to know the index of the display you want to know about. I have not played that much with this part of SDL 2, but I guess you can use SDL_GetNumVideoDisplays to get the number of displays (and check if there is at least one ? - I think the SDL_Window part might not work if there is no display available anyway) and choose one. Or you could pick the first one, which has index 0.

Oh, and you can look at the exemple on the page of SDL_GetCurrentDisplayMode, they effectively retreive the size of a display.

Friday, July 30, 2021
 
Aniket Thakur
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share