Asked  7 Months ago    Answers:  5   Viewed   41 times

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



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:


$(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!');

PHP (some_script.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
answered 7 Months ago

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

  1. Use (or localhost) on both sides
  2. Use the hostname on both sides
Saturday, May 29, 2021
answered 5 Months ago

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
answered 5 Months ago

why dont you keep it simple and clean

    $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
answered 5 Months ago

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 :