Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Determining Client Screen width for use in PHP

  1. #1
    Join Date
    Mar 2007
    Location
    TN, US
    Beans
    607
    Distro
    Ubuntu

    Determining Client Screen width for use in PHP

    Hello, I am trying to figure out how to determine a User's screen width in Javascript, and have it available in PHP as a variable. I have done a bit of research, and I think that for my purposes this could be best accomplished by setting a cookie, but I have almost no knowledge of Javascript, and my PHP is weak. To make things slightly more complicated, I plan on putting this code in my head.php file, which is included in all of the pages on my site, so if setting a cookie requires that the page be refreshed, we will need to figure out how to do that to the correct page. Any help would be greatly appreciated!
    Registered Linux user 446122 , Registered Machine 352936.

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Determining Client Screen width for use in PHP

    Quote Originally Posted by brennydoogles View Post
    Hello, I am trying to figure out how to determine a User's screen width in Javascript, and have it available in PHP as a variable. I have done a bit of research, and I think that for my purposes this could be best accomplished by setting a cookie, but I have almost no knowledge of Javascript, and my PHP is weak. To make things slightly more complicated, I plan on putting this code in my head.php file, which is included in all of the pages on my site, so if setting a cookie requires that the page be refreshed, we will need to figure out how to do that to the correct page. Any help would be greatly appreciated!
    Such calculations should be done on the client, not server.

    PHP Code:
    screen.height;
    screen.width
    Would be the ECMAScript for it, and you could send this in a form, or use it with Ajax.

  3. #3
    Join Date
    Mar 2007
    Location
    TN, US
    Beans
    607
    Distro
    Ubuntu

    Re: Determining Client Screen width for use in PHP

    Quote Originally Posted by LaRoza View Post
    Such calculations should be done on the client, not server.

    PHP Code:
    screen.height;
    screen.width
    Would be the ECMAScript for it, and you could send this in a form, or use it with Ajax.
    I understand that I would need to do this with Javascript, but my problem is that I don't know enough Javascript to make it work correctly. I was hoping to set the values as a cookie so that they may be easily accessed by PHP, but if there is a way to do this easily without relying on cookies I would be more than open to it. Any help would be awesome!
    Registered Linux user 446122 , Registered Machine 352936.

  4. #4
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Determining Client Screen width for use in PHP

    Quote Originally Posted by brennydoogles View Post
    I understand that I would need to do this with Javascript, but my problem is that I don't know enough Javascript to make it work correctly. I was hoping to set the values as a cookie so that they may be easily accessed by PHP, but if there is a way to do this easily without relying on cookies I would be more than open to it. Any help would be awesome!
    Use Ajax, or use the ECMAScript to set it in a form to submit.

  5. #5
    Join Date
    Mar 2007
    Location
    TN, US
    Beans
    607
    Distro
    Ubuntu

    Re: Determining Client Screen width for use in PHP

    Quote Originally Posted by LaRoza View Post
    Use Ajax, or use the ECMAScript to set it in a form to submit.
    I guess the real question I'm getting at is... How would I do that? I don't think a form is a good option for me, since I will be using this to make determinations about layout based upon the users screen width. As for the AJAX, I only have the most vague clue on how it actually works, so writing the code is far beyond my grasp. I am more than willing to learn, but I also would like to be able to get started on my project ASAP. Thanks!
    Registered Linux user 446122 , Registered Machine 352936.

  6. #6
    Join Date
    Dec 2005
    Location
    Lithuania / Vilnius
    Beans
    26
    Distro
    Ubuntu 13.04 Raring Ringtail

    Thumbs down Re: Determining Client Screen width for use in PHP

    The most stimple way would be read screen size with JavaScript and redirect page with parameters.

    HTML Code:
    <script type="text/javascript">
    window.onload  = function() {
            alert(screen.height);
            window.location = "http://www.somepage.com/index.php?h=" + screen.height + "&" + screen.width;
    }
    </script>
    But this way is not very cute. I don't like parameters in URLs Also GET method should be used only to get data or format query, not to sending data. You can use AJAX. For AJAX you can try script.aculo.us or jQuery JavaScript libraries. There you'll find simple AJAX tutorials.

    If you want use cookies, look at this page: http://www.quirksmode.org/js/cookies.html

    Be careful and escape your input data
    Last edited by Pawka; June 12th, 2008 at 02:46 PM.

  7. #7
    Join Date
    Dec 2007
    Beans
    81
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: Determining Client Screen width for use in PHP

    jQuery, as Pawka mentioned, would also work great to make layout changes on the fly too without having to go between the client and server and rerender the pages.

    Here is the delemna you will have. You will have to load a page that will return that info (even if you store it in a cookie) so that you know how to render the page. In which case, having a form do this isn't a bad option. You need to have that initial page anyway, does it matter if it's a form?

  8. #8
    Join Date
    Mar 2007
    Location
    TN, US
    Beans
    607
    Distro
    Ubuntu

    Re: Determining Client Screen width for use in PHP

    Quote Originally Posted by MacAnthony View Post
    jQuery, as Pawka mentioned, would also work great to make layout changes on the fly too without having to go between the client and server and rerender the pages.

    Here is the delemna you will have. You will have to load a page that will return that info (even if you store it in a cookie) so that you know how to render the page. In which case, having a form do this isn't a bad option. You need to have that initial page anyway, does it matter if it's a form?
    Well, I would think that a form would require the user to do something in order to submit the values, meaning inconvenience to the visitors. Am I wrong about this? It seems easier to have the site set a cookie once, and then redirect immediately and not have to do it again unless the cache gets cleared or the cookie expires.
    Registered Linux user 446122 , Registered Machine 352936.

  9. #9
    Join Date
    Dec 2007
    Beans
    81
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: Determining Client Screen width for use in PHP

    You could automatically submit a form just as easily as you could set a cookie and redirect the page via javascript. All personal preference on how to handle it.

  10. #10
    Join Date
    Mar 2007
    Location
    TN, US
    Beans
    607
    Distro
    Ubuntu

    Re: Determining Client Screen width for use in PHP

    Quote Originally Posted by MacAnthony View Post
    You could automatically submit a form just as easily as you could set a cookie and redirect the page via javascript. All personal preference on how to handle it.
    Interesting. I'm not sure how to do that (if you couldn't tell by the fact I didn't know it was possible), but that sounds like a good option. How would I go about doing it?
    Registered Linux user 446122 , Registered Machine 352936.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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