Getting the screen resolution using PHP

前端 未结 21 2247
你的背包
你的背包 2020-11-22 06:50

I need to find the screen resolution of a users screen who visits my website?

21条回答
  •  忘了有多久
    2020-11-22 07:07

    This can be done easily using cookies. This method allows the page to check the stored cookie values against the screen height and width (or browser view port height and width values), and if they are different it will reset the cookie and reload the page. The code needs to allow for user preferences. If persistant cookies are turned off, use a session cookie. If that doesn't work you have to go with a default setting.

    1. Javascript: Check if height & width cookie set
    2. Javascript: If set, check if screen.height & screen.width (or whatever you want) matches the current value of the cookie
    3. Javascript: If cookie not set or it does not match the current value, then: a. Javascript: create persistent or session cookie named (e.g.) 'shw' to value of current screen.height & screen.width.
      b. Javascript: redirect to SELF using window.location.reload(). When it reloads, it will skip the step 3.
    4. PHP: $_COOKIE['shw'] contains values.
    5. Continue with PHP

    E.g., I am using some common cookie functions found on the web. Make sure setCookie returns the correct values. I put this code immediately after the head tag. Obviously the function should be in a a source file.

    
    
    
    ....
    

提交回复
热议问题