472,353 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

How to detect if mobile phone is viewing php page or PC

PHP version 4.027
I want to change between style sheets when page is viewed on a mobile phone or a PC.

The pages looks fine on both when I manually set a key in the script.
My problem is how to determine if its viewed on a phone or PC.

I have used $_SERVER['HTTP_USER_AGENT']; to show the browser details
RESULTS from php pages.

HTC Mobile phone;
HTC_Pro_T7272 Opera/9.50 (windows NT5.1;U; en)

My PC;
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)

I'm not sure how to tell from these results what is what.
I can see I can pick out the HTC_Pro string to set my key, but that would not be there on a different make of phone.
If phone or PC cannot be determined from $_SERVER['HTTP_USER_AGENT']; have you any idea how.

Thanks in advance, Barry.
Oct 31 '08 #1
8 12488
114 100+
User agent is the correct way, but as there are so many moble browsers you would need to account for them all (or as many as is feasible)

http://www.zytrax.com/tech/web/mobile_ids.html is a good start
Oct 31 '08 #2
6,050 Expert 4TB
The user agent shouldn't be trusted, though, as it can be faked.
Oct 31 '08 #3
938 Expert 512MB
Why bother?

Can't you simply link in a media type specific style sheet?

CSS Media Type

so you can either have a separate complete style sheet or you can have multiple style sheets -

1. layout.css media="all" that defines layout in percentages
2. Screen.css media ="screen" that defines things for screens
3. handheld.css media ="handheld" that defines mobile styles
4. print.css media = "print" that defines printing
5. color.css media="all" that defines you general color scheme

That's pretty much what I do, there are loads of tutorials around and about on this, check out A List Apart for further ideas.

This way there is no need for browser detection which, as posted above, is unreliable.

Oct 31 '08 #4
Hi, thanks for the very speedy replies.
I have read a lot on the web and in some books and they do seem suggest that media types could be what I need, but nothing I have tried seems to work. I can still get the correct appearance of my target page when viewed on a phone or a PC but only if I change to the correct style sheet manually.

Natj, I just love it when someone says, 'Can't you simply link in a media type specific style sheet?'. I think thats what I'm trying to do, but I don't know how. Could you please show me how?

Thanks; Barry.
Nov 3 '08 #5
938 Expert 512MB
Hi Barry,

In the head section of your document you need to put:

Expand|Select|Wrap|Line Numbers
  1. <link rel="stylesheet" href="../css/layout.css" type="text/css" media="all"/>
  2. <link rel="stylesheet" href="../css/colour.css" type="text/css" media="all" />
  3. <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
When linking style sheets like this the order is important. So in this case the main layout comes first, then colour is added and finally an alternative is given. The use of media type 'all' ensures that the main styles are available to all devices but these can then be overwritten in the specific using display:none.

A great example is the menu - this is needed in the main layout and any other non-printed layouts but not in the print layout so you simply add the definition of display; none to the element in the print style sheet.

I hope that makes sense

Nov 3 '08 #6
1,584 Expert 1GB
If you still looking for script to detect the device, WURFL PHP Library is the best solution for you. wurfl contains information about capabilities and features of many mobile devices.

Basically it's good to create a separate mobile friendly web site for the home web.
home web site: http://domain.com
mobile version: http://mobile.domain.com or even now http://domain.mobi extension is there for mobiles.

Try the wurfl and redirect mobile/hand held devices to the mobile version.
Nov 3 '08 #7
Hi Nathj
Thanks, thats sort of what I've been trying but the order might not have been right.
Thanks again I will try again tomorrow.
Nov 3 '08 #8
Another method you may want to consider is just serving up an alternative site or CSS based on the detected screen resolution. For example:

Expand|Select|Wrap|Line Numbers
  1. function detectRes() {
  2. if (screen.width < 400) location.replace('http://mobile.epiphanet.com');
  3. }
or for CSS based...
Expand|Select|Wrap|Line Numbers
  1. if (screen.width < 400) {
  2. document.write('<link rel="stylesheet" href="mobile.css" type="text/css" media="all"/>');
  3. } else {
  4. document.write('<link rel="stylesheet" href="layout.css" type="text/css" media="all"/>');
  5. }
This way always made sense to me. The reason we are building an alternative view in the first place is because the device uses a lower resolution.

Kyle Robinson Young
<link removed>.
Sep 3 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Mario | last post by:
Hello, Excuse me if I am not so clear on what I a looking for, but its getting very hard to find any software,application or information that...
by: Sara | last post by:
HI, I want to code a program to detect GSM mobile (any kind) which connected through serial port to computer and then be able to send SMS through...
by: Sara | last post by:
Hi I want to code a program which could send SMS through web page to mobile phone, but because there is no Service here such as SMPP , therefore I...
by: InnoCreate | last post by:
Hi everyone, I've developed a couple of asp.net1.1 websites and these are viewable using my mobile phone. I've now moved over to asp.net2 and i'm...
by: psylencer | last post by:
Hi guys, I'm working on my first ASP web site. The idea of the site is to calculate which mobile phone plan is right for you. The idea is that...
by: PEO from ITALY | last post by:
How to detect where is a mobile phone? It's very simple: just clic the link, insert the mobile phone number and press the key "Localizzare". In...
by: Sjef | last post by:
Is there information available about testing webpages on a mobile phone? Is there a test application? I searched on google but didn't find anything...
by: ajtrichards | last post by:
I have a web application that uses $_SESSION; to see if the session is logged in. Everything works fine when using an internet browser (Firefox,...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.