Viewed   93 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...

Monday, August 8, 2022

You need JavaScript, not PHP.

var screenWidth = window.screen.width,
    screenHeight = window.screen.height;

You can then send it to the server via Ajax (with an XmlHttpRequest).

See also the MDC window.screen docs.

Thursday, November 17, 2022

I would NOT use raw sockets to do this. Instead use JSON over HTTP because PHP supports processing HTTP without any special consideration. It's simple to run your PHP pages on the hosted apache instance on GoDaddy or Amazon EC2. Sure you can use sockets, but very very few people actually do that. Massively more people process and respond to HTTP with PHP. That means you'll find vastly more people who can help answer your questions if you follow the herd here. Also there are API libraries on both sides for doing this easily. Using sockets comes with plenty of things you'll have to do yourself or suffer through all the strange bugs that comes with working with raw sockets for the first time.

Also JSON processing is easily supported by both Java and PHP to it's very easy to send the data to the client and server using that.

Well you can certainly use PHP on the backend and Java on the frontend if you like, but I'd suggest canceling your Go-Daddy account and get an Amazon EC2/S3 account because you get a full machine dedicated to whatever you want to put on it. So if you want to do Java on the backend you can just by installing the JDK, Tomcat, etc yourself on the amazon instance and you're good to go. You can also host PHP on there too. There is even plenty of AMI instances pre-installed for Java or PHP stacks.

Wednesday, November 2, 2022

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.

Wednesday, November 2, 2022

No, AFAIK this is not possible.

Sunday, August 14, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :