I have a script (JAVA + PHP) that will determine a web users screen resolution. After the JAVA picks it up, the PHP displays a message to the extent of "Your screen resolution is 1024 X 768".

What I want to do is to pick up the variable for screen width and write an if statement in PHP so that my page determines the screen width of the users resolution and decides which background image to display based on the width instead of displaying a message.

Any help/hints in this would be greatly appreciated and I can provide the source I am building on this evening if that helps.

    Are you talking about Java or JavaScript? Without knowing that it is impossible to help you. I would guess that it is JavaScript since I can't see a reason to use Java and PHP together.

      Piranha wrote:

      Are you talking about Java or JavaScript? Without knowing that it is impossible to help you. I would guess that it is JavaScript since I can't see a reason to use Java and PHP together.

      Sorry- yes, it is JavaScript. Basically the script will get the resolution and put it into two variables, Screen.width and Screen.height. The one I want is Screen.width and based on the value of Screen.width I want to select which background pic to display for the user.

        Here is the actual script I am starting with:

        ?>

        <script language="javascript">
        <!--
        function writeCookie()
        {
        var today = new Date();
        var the_date = new Date("December 31, 2023");
        var the_cookie_date = the_date.toGMTString();
        var the_cookie = "users_res="+ screen.width +"x"+ screen.height;
        var the_cookie = the_cookie + ";expires=" + the_cookie_date;
        document.cookie=the_cookie
        if (document.cookie){

        location = '<?=$GLOBALS[callget_res_page_name]?>';
        }

        }

        function checkRes(width, height) {
        if(width != screen.width || height != screen.height) {
        writeCookie();
        } else {
        return true;
        }
        }
        //-->
        </script>

        <?
        if(isset($HTTP_COOKIE_VARS["users_res"])) {
        $screen_res = $HTTP_COOKIE_VARS["users_res"];
        $screen_res_tmp = explode("x", $screen_res);
        $screen_width = $screen_res_tmp[0];
        $screen_height = $screen_res_tmp[1];
        ?>
        <script language="javascript">
        <!--
        checkRes(<?=$screen_width?>, <?=$screen_height?>);
        //-->
        </script>
        <?
        }
        else //means cookie is not found set it using Javascript
        {
        ?>
        <script language="javascript">
        <!--
        writeCookie();
        //-->
        </script>
        <?
        }
        ?>

          Hi.

          Easy answer: Don't do this. Rethink your design

          The screen width does not tell you the size of the browser, the number of plugins or side-bars. Stick with fluid designs.

          You can pass it through AJAX (Which I have not used) of by reloading the page, but with the javascript-derived variables in the url:

          page.php?width='+width'

          Which are then available in the re-submitted page from the GET array:

          $width = $_GET['width'];

          That is the principle. As I do not like javascript, I cannot give you a working snippet; I am not very good at it.

            leatherback wrote:

            Hi.

            Easy answer: Don't do this. Rethink your design

            The screen width does not tell you the size of the browser, the number of plugins or side-bars. Stick with fluid designs.

            You can pass it through AJAX (Which I have not used) of by reloading the page, but with the javascript-derived variables in the url:

            page.php?width='+width'

            Which are then available in the re-submitted page from the GET array:

            $width = $_GET['width'];

            That is the principle. As I do not like javascript, I cannot give you a working snippet; I am not very good at it.

            Thanks for your thoughts; you have valid points and I'll take a look at that solution too. I still want to put this together for the time being though as answers to this will help me to understand PHP in other areas as well.

              If it is only the background image that is changing with the resulution I suggest that you let JavaScript choose image for you. Otherwise it is Ajax or one or more page reloads that could work.

                Piranha wrote:

                If it is only the background image that is changing with the resulution I suggest that you let JavaScript choose image for you. Otherwise it is Ajax or one or more page reloads that could work.

                I had thought of that, but thought that I needed to have PHP 'pick up' the variable so that it would display the correct background via a variable in the header file.

                I'll research the Java option though; I am assuming then that I could just include the JavaScript in the header and get the res then write an if statement to act on it instead of having the script in a separate file?

                Thanks for looking at this Piranha.

                  Sorry to not answer your question, but to divert you from it:

                  Yeah, I end up calling the background image with javascript when I have to swap the images based on the sort of information.

                    Will Poulson wrote:

                    Sorry to not answer your question, but to divert you from it:

                    Yeah, I end up calling the background image with javascript when I have to swap the images based on the sort of information.

                    You did answer my question- thanks for that! I ended up finding a bit of javascript that I was able to modify and it worked great. I'll grab the code later and post it for everyone.

                    Thanks to all who replied to this- much appreciated, and I learned from the experience!

                      Write a Reply...