+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Check Screen Resolution With PHP

  1. #1
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Check Screen Resolution With PHP

    i need to check the resolution of the users screen and then change a variable based on that.
    i think that his code would work, but i don't know.
    could someone look it over please.

    PHP Code:
    <?php
    $width 
    "
    ?>
    <script type="
    text/javascript">
    document.write = 'screen.width';
    </script>
    <?php
    "
    ;
    ?>
     
    thanks for your help!

  2. #2
    konekt's Avatar
    konekt is offline x10 Sophmore konekt is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    100

    Re: Check Screen Resolution With PHP

    This will give you the width:

    Code:
    $width = " <script>document.write(screen.width); </script>";
    You, however, can not manipulate width by manipulating that variable (and you shouldn't be changing screen sizes anyway).

  3. #3
    MasterMax1313's Avatar
    MasterMax1313 is offline x10Hosting Member MasterMax1313 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    84

    Re: Check Screen Resolution With PHP

    I'm not sure that will actually work, since the php is executed server side, and the javascript is executed client side. so I would think that you would need to use javascript client side to maybe use AJAX to post back to the webserver the screen size, and maybe save it in a session variable so that it is then available for the next page.
    If you found this helpful, don't be afraid to add to my rep
    ____________________________

    There are only 10 types of people in this world...

    Those who understand binary,
    And those who don't

  4. #4
    konekt's Avatar
    konekt is offline x10 Sophmore konekt is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    100

    Re: Check Screen Resolution With PHP

    Quote Originally Posted by MasterMax1313 View Post
    I'm not sure that will actually work, since the php is executed server side, and the javascript is executed client side. so I would think that you would need to use javascript client side to maybe use AJAX to post back to the webserver the screen size, and maybe save it in a session variable so that it is then available for the next page.
    It will work, I tried it out.

  5. #5
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: Check Screen Resolution With PHP

    I don't think that would work unless I'm misunderstanding what the OP wants to do. I assume he wants to get the client's resolution and make layout adjustments depending on it, such as deciding on which stylesheet to use. If this is the case, then I would personally recommend having JS get the width and height on the user's first visit. Then it should set a couple of cookies with these values and reload the page. PHP should then be able to access these values through the $_COOKIES global array.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  6. #6
    konekt's Avatar
    konekt is offline x10 Sophmore konekt is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    100

    Re: Check Screen Resolution With PHP

    This gives your width screen width (not document width):
    http://konekt.x10hosting.com/test.php
    Last edited by konekt; 04-14-2008 at 05:29 PM.

  7. #7
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: Check Screen Resolution With PHP

    I don't believe he wants the resolution outputted because he said he wanted to change a variable based on the client's resolution. So I'm fairly certain he wants a way for php to get the client's resolution and use it in his script.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  8. #8
    konekt's Avatar
    konekt is offline x10 Sophmore konekt is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    100

    Re: Check Screen Resolution With PHP

    Quote Originally Posted by woiwky View Post
    I don't believe he wants the resolution outputted because he said he wanted to change a variable based on the client's resolution. So I'm fairly certain he wants a way for php to get the client's resolution and use it in his script.
    You'll have to forgive me for not seeing the difference between the two. Outputting a variable is having the information and variable. He can use the outputted information like any php variable...

  9. #9
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: Check Screen Resolution With PHP

    There is a difference when JS is making the output instead of PHP. If you output JS code, the result is unknown to the server since it's only going to be executed on the client. If you set a variable to some JS code, php just sees it as a string. Try doing this with that $width variable from before:

    PHP Code:
    $width "<script>document.write(screen.width); </script>";
    if (
    $width 100) {
        print 
    'width is greater than 100';

    That print statement would never execute. This is what I meant by that code wouldn't work for what I believe kbjr has in mind.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  10. #10
    konekt's Avatar
    konekt is offline x10 Sophmore konekt is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    100

    Re: Check Screen Resolution With PHP

    Quote Originally Posted by woiwky View Post
    There is a difference when JS is making the output instead of PHP. If you output JS code, the result is unknown to the server since it's only going to be executed on the client. If you set a variable to some JS code, php just sees it as a string. Try doing this with that $width variable from before:

    PHP Code:
    $width "<script>document.write(screen.width); </script>";
    if (
    $width 100) {
        print 
    'width is greater than 100';

    That print statement would never execute. This is what I meant by that code wouldn't work for what I believe kbjr has in mind.
    Yeah, you are right, I apologize. It's been a while since I've had to do something like this, and I remembered the latter attempt working.

    If anyone is looking for a reason why this is, it is because the php is calling the JS which is doing the outputting. The echo statement is not doing any outputting, it's just calling on the javascript to do the writing. The $var then becomes a command almost. Thus the value of the var is the command.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. SUHOSIN - Use of eval is forbidden
    By MaestroFX1 in forum Free Hosting
    Replies: 19
    Last Post: 03-07-2008, 02:42 AM
  2. Free Development Web Software
    By Danfelbm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 03-01-2008, 07:01 PM
  3. Important PHP Information
    By Bryon in forum News and Announcements
    Replies: 0
    Last Post: 11-21-2007, 02:08 PM
  4. [PHP] Placing the Ads in PHP Pages
    By Chris in forum Tutorials
    Replies: 22
    Last Post: 06-04-2005, 11:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers