Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
how to get screen width in php? (may be pc, mobile phone..)
Posted
Updated 15-Jan-23 22:52pm
Comments
Sergey Alexandrovich Kryukov 10-Dec-11 17:21pm    
Is Google your enemy?
--SA
tdytklf 11-Dec-11 11:00am    
han?
tdytklf 11-Dec-11 17:13pm    
oh! so.. i did not know that there is something called"goolge" you said? that! thank you!
-
the truth is that i want to meet code friends and get the best answers then become a CodMan myself!!
tdytklf 11-Dec-11 17:17pm    
:|:
Great Thanks to engineer SA.

about the solution- sorry. i'm a beginner!

PHP itself works only on server side and does not know anything about client system except information sent to the server side by a client side in some HTTP request. That said, using JavaScript to gather required information of the current client environment like window size is unavoidable.

I presume the inner window width is required, because this is the client width determining the design of the content.

Now, JavaScript gets information on the window width via the property window.innerWidth. This is a standard property, see http://www.w3schools.com/jsref/obj_window.asp[^], but there are several kinds of incompatibilities like in IE, see for example http://www.javascriptkit.com/javatutors/static2.shtml[^], http://www.javascripter.net/faq/browserw.htm[^].

To avoid incompatibilities, I would use jQuery $(window).innerWidth(). It's important not to forget round brackets at the end, as in jQuery this is a function.

See http://api.jquery.com/innerWidth/[^].

The server side can get this information in different ways; the selection of the method should depend on the design and logic of your application. It could be sent as a form submission, AJAX or URL parameters.

onwayir wrote:
Thanks, but the problem is that some mobile systems — some platforms do not support JavaScript.
At last what is the professional suggestion? (js-php or some better way?)
Lots of thanks. [OP's grammar/spelling/capitalization fixed — SA]


First, please don't post a solution, unless this is really a solution. In all other cases, use "Improve question" above, add a comment or reply to existing comment.

I don't know what's "js-php", I suspect this is nothing new, same very JavaScript combined with PHP. I don't really know browsers without PHP (except text-only ones), but the truth is, JavaScript can be even switched off. In this case, you are out of luck — the only universally accepted standard on client side is EMCAScript implemented as JavaScript. I think I already explained above why you cannot know such client-specific detail without using JavaScript.

So, what could you do. I would say, use more simple and reliable HTML/CSS design. Imagine you only use paragraphs and headers, tweak only the font styles and margin/padding styles, no absolute positioning. Will it be screen-size independent? Yes, pretty much. Such format is fluid, it will adopt to screen size itself. Likewise, you can all a lot more formatting in such device-independent manner and still be able to achieve quite a rich UI. Just keep it in mind and permanently test on different windows sizes, simply resizing your browser window.

As a free bonus, such design will be highly tolerable to browser incompatibility. And don't be afraid to be not enough impressive to the users. Most reasonable people were already fed up with beefed-up "cool" design. People would be more happy with highly readable and functional design, not highly graphical puzzles.

—SA
 
Share this answer
 
v6
function CurrentWidth() {
$CurrentWidth = " var mobilewidth = screen.width; if(mobilewidth < 1400){ document.write('smallDesktop'); }else{ document.write('largeDesktop'); } ";
echo $CurrentWidth;
}

// Please place CurrentWidth() function into your functions.php
//You can use your custom width instead of 1400
// Usage: echo CurrentWidth() return your value
 
Share this answer
 
Comments
Richard Deeming 16-Jan-23 5:25am    
Or better yet, as solution 1 mentioned eleven years ago, don't build a website design that depends on the precise width of the browser.
Responsive web design basics[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900