Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Detect screen resolution with Javascript and PHP

    I'm trying to find a script that will detect the client's screen size so that I can do something like:

    PHP Code:
    if ($screen_width 1024){
       
    function1();
    else {
      
    function2();

    I've tried these examples:

    http://www.wallpaperama.com/forums/p...ript-t517.html

    http://www.phpbuddy.com/article.php?id=8

    But my php page just keeps loading and nothing is displayed. It looks like its stuck in a loop? My guess is its not writing the cookie.

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    This should work fine.

    PHP Code:
    <HTML>
    <TITLE>screen resolution</TITLE>
    <HEAD>
    <?
    if(isset($HTTP_COOKIE_VARS["users_resolution"]))
    $screen_res $HTTP_COOKIE_VARS["users_resolution"];
    else 
    //means cookie is not found set it using Javascript
    {
    ?>
    <script language="javascript">
    <!--
    writeCookie();

    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_resolution="+ screen.width +"x"+ screen.height;
    var the_cookie = the_cookie + ";expires=" + the_cookie_date;
    document.cookie=the_cookie

    location = 'get_resolution.php';
    }
    //-->
    </script>
    <?
    }
    ?>
    </HEAD>
    <BODY>
    <?php
    echo "Your screen resolution is set at "$screen_res;
    ?>
    </BODY>
    </HTML>
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    136
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Or you could just pass the resolution from a Javascript page using $_GET which is less attractive but easier:

    If there is no info in $_GET do this:

    Code:
    window.location.href = 'thispage.php?w=' + screen.width + '&h=' + screen.height;
    Else do this:

    PHP Code:
    $height=$_GET['h'];
    $width=$_GET['w']; 
    Then do what you want with that information.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, in either case you'll need JS to detect this; PHP has no idea what a screen is. Lol.
    With Masterophollies suggestion, I'd actually follow that with JUST JS, no PHP at all. Also, PHP should use the $_COOKIE superglobal; HTTP_*_VARS are officially gone as of PHP6, and are currently disableable.
    But, I do agree with cookies being used. Just use straight JS, since we know for 100% certainty that it will exist.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by masterofollies View Post
    This should work fine.

    PHP Code:
    <HTML>
    <TITLE>screen resolution</TITLE>
    <HEAD>
    <?
    if(isset($HTTP_COOKIE_VARS["users_resolution"]))
    $screen_res $HTTP_COOKIE_VARS["users_resolution"];
    else 
    //means cookie is not found set it using Javascript
    {
    ?>
    <script language="javascript">
    <!--
    writeCookie();

    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_resolution="+ screen.width +"x"+ screen.height;
    var the_cookie = the_cookie + ";expires=" + the_cookie_date;
    document.cookie=the_cookie

    location = 'get_resolution.php';
    }
    //-->
    </script>
    <?
    }
    ?>
    </HEAD>
    <BODY>
    <?php
    echo "Your screen resolution is set at "$screen_res;
    ?>
    </BODY>
    </HTML>
    I'd like to go with the JS/PHP solution as I can then build in my PHP functions.
    However I cant get this code to work.
    I've tried it on my office server and now at home using XAMPP. Both just go into some sort of loop. The status says 'waiting for 127.0.0.1', then 'stopped', and the loop continues.

    I've looked at my cookies and one has been set with content of "1280x1024", so that part is working.

    I commented out location = 'get_resolution.php'; to stop the forwarding loop back onto itself. As I know the cookie is set, it should still display the screen resolution, however it doesn't, just 'Your screen resolution is set at '.

    So it looks like a problem reading back the cookie?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Did you refresh the page? Cookies of course do not show up until the next load.
    Also, did you change the $HTTP_COOKIE_VARS to $_COOKIE?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Found the problem.

    It was a combination of not using the $_COOKIE Superglobal and not using <? instead of <?php.

    I'm guessing the example was written in PHP4?

    Here's the amended code:

    PHP Code:
    <HTML>
    <TITLE>screen resolution</TITLE>
    <HEAD>
    </HEAD>
    <BODY>

    <?php
    if(isset($_COOKIE["users_resolution"]))
    $screen_res $_COOKIE["users_resolution"];
    else 
    //means cookie is not found set it using Javascript
    {
    ?>
    <script language="javascript">
    <!--
    writeCookie();

    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_resolution="+ screen.width +"x"+ screen.height;
    var the_cookie = the_cookie + ";expires=" + the_cookie_date;
    document.cookie=the_cookie

    location = 'get_resolution.php';
    }
    //-->
    </script>
    <?php
    }
    ?>


    <?php
    echo "Your screen resolution is set at "$screen_res;
    ?>
    </BODY>
    </HTML>

  • #8
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I'm not too sure when to set the cookie to expire.
    The problem is people can change their monitor and/or screen resolution at any time.

    Does anyone have a recommendation on this?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The code could have been written for pretty much any version; the difference is that you're server doesn't have short_tags enabled. Which is ok, I always recommend not using them for just these reasons.

    As for detecting changes, only javascript can do that. Instead of using PHP to check that, use pure javascript and alter you're function to check if its cookies are not existant OR the sizes have changed compared to the cookie, update them.
    And as mentioned, they will still only take effect after the next page load.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #10
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    That all sounds good to me... but I haven't a clue how to do this. Can you help?

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Nope sorry. JS cookies are one big string, so you'll need to ask a JS writer for that; my JS skills are subpar.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #12
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks anyway.
    I'll hop over to the Javscript forum now I know its Javascript I'm needing.

  • #13
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    @Fou-Lu

    we know for 100% certainty that it will exist.
    That what will exist?
    I must have missed something, but that statement confused me.


    PS If "Fou-Lu" is Chinese (is it?), what are the tones on it? I was trying to guess the meaning, but there are so many possibilities!

  • #14
    Regular Coder
    Join Date
    Jul 2009
    Posts
    136
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Or you could use my approach which means copy and pasting one line of Javascript into SCRIPT tags and then using PHP to $_GET it. That's why I suggested it as you don't need any Javascript knowledge for that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •