Quantcast

Forums | MacLife

You are not logged in.

#1 2008-03-12 11:41 pm

PHUSMC
One thumbed gimpy
From: Kent, WA
Registered: 2006-05-14
Posts: 822
Website

Detecting an iPhone?

So I thought it would be fun to experiment with making a site designed for the iPhone and looked up a few examples of how to detect the iPhone user agent.  I tried to do this in JavaScript and in PHP but neither seemed to work.  Anyone have any insight as to why this won't work?

PHP:

Code:

<?php
$browser = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone")
if ($browser)
{
    echo 'You are using an iPhone';
}
else
{
    echo 'You are not using an iPhone';
}
?>

MacBook Pro 15.4"
2.5GHz CPU, 250GB HDD, 512MB VRAM

Offline

 

#2 2008-03-17 3:33 pm

thumbprint
giant member
Registered: 2003-06-22
Posts: 172
Website

Re: Detecting an iPhone?

Sorry I don't have the answer, but am interested in finding out, too...

I do know that Apple just recently released an iPhone SDK and some of it pertains to web developers.  I haven't been able to look through the SDK because it's only compatible with Leopard (I've got Tiger).

http://developer.apple.com/iphone/program/

Last edited by thumbprint (2008-03-17 3:35 pm)


- Thumbprint

Offline

 

#3 2008-03-17 6:12 pm

matt
a very bad matt
Registered: 1999-09-16
Posts: 16685
Website

Re: Detecting an iPhone?

Is your server using php5?

Try strpos() instead of stripos().


Being loud: The next best thing to being right.

Do not click here.

Offline

 

#4 2008-03-18 8:24 am

Alien
Forum Czar
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16939
Website

Re: Detecting an iPhone?

Code:

echo 'You are '. (strpos($_SERVER['HTTP_USER_AGENT'],"iPhone") ? '' : 'not ') . 'using an iPhone.';

Don't they teach you kids to code efficiently, these days?

Sheesh.

.tsooJ


http://macstack.net/forums/images/smilies/lol.gif

Offline

 

#5 2008-03-19 1:22 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: Detecting an iPhone?

Ternaries are for pussies. wink

Offline

 

#6 2008-03-19 6:13 am

Alien
Forum Czar
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16939
Website

Re: Detecting an iPhone?

I'm a mathematician and a programmer. Nested single line statements give me a boner. tongue

.tsooJ


http://macstack.net/forums/images/smilies/lol.gif

Offline

 

#7 2009-04-01 12:10 pm

Nefarious
Tuning Fork
Moderator
From: 45°22"N 84°57"W
Registered: 2002-09-30
Posts: 7996

Re: Detecting an iPhone?

okay fans....   Javascript newbie here.

In detecting an iPhone:

I would like to have 2 versions of a webpage that I am making.   Would there be 2 webpages ? or both versions in one webpage.    I have GoDaddy hosting, so it seems I have PHP.

Here's are webpages that I have been looking at.  http://davidwalsh.name/detect-iphone
http://www.quirksmode.org/js/detect.html

After detecting the iPhone, how do I transfer automatically to the desired webpage ?

Offline

 

#8 2009-04-01 7:19 pm

Basseq
Fixxy of F&S fame
From: D.C.
Registered: 2002-12-18
Posts: 3125
Website

Re: Detecting an iPhone?

Code:

document.location

or...

Code:

header('location: ...');

Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)

Offline

 

#9 2009-04-01 8:06 pm

Nefarious
Tuning Fork
Moderator
From: 45°22"N 84°57"W
Registered: 2002-09-30
Posts: 7996

Re: Detecting an iPhone?

Thx

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    document.location.href="mainiphone.html";

}

Offline

 

#10 2009-04-01 8:35 pm

registered_user
bulletproof
From: padding: zero-pixels;
Registered: 2000-12-19
Posts: 16026
Website

Re: Detecting an iPhone?

I've written a little javascript that detects iPhones or iPods.  I hope you find it useful.

Code:

var iPhone = false;
var iPod = false;

var nibble = '';
var agent = navigator.userAgent.toString();

// first check if it's an iPhone
for(var i=0; i<agent.length; i++) {
    if(agent.charAt(i) == "i") {
        nibble += 'i';
    }
    if(agent.charAt(i) == "P" && nibble == "i") {
        nibble += 'P';
    } else {
        nibble = '';
    }
    if(agent.charAt(i) == "h" && nibble == "iP") {
        nibble += 'h';
    } else {
        nibble = '';
    }
    if(agent.charAt(i) == "o" && nibble == "iPh") {
        nibble += 'o';
    } else {
        nibble = '';
    }
    if(agent.charAt(i) == "n" && nibble == "iPho") {
        nibble += 'n';
    } else {
        nibble = '';
    }
    if(agent.charAt(i) == "e" && nibble == "iPhon") {
        nibble += 'e';
        iPhone = true;
    } else {
        nibble = '';
    }
}

// if it's not an iPhone, let's see if it's an iPod
if(!iPhone) {
    for(var i=0; i<agent.length; i++) {
        if(agent.charAt(i) == "i") {
            nibble += 'i';
        }
        if(agent.charAt(i) == "P" && nibble == "i") {
            nibble += 'P';
        } else {
            nibble = '';
        }
        if(agent.charAt(i) == "o" && nibble == "iP") {
            nibble += 'o';
        } else {
            nibble = '';
        }
        if(agent.charAt(i) == "d" && nibble == "iPo") {
            nibble += 'd';
            iPod = true;
        } else {
            nibble = '';
        }
    }

}


if(iPhone || iPod) {
    if(iPhone) {
        alert("this is an iPhone");
    } else {
        alert("this is an iPod");
    }
} else {
    alert("this is not an iPhone");
}

Last edited by registered_user (2009-04-01 8:35 pm)

Offline

 

Board footer

Powered by PunBB 1.2.6
© Copyright 2002–2005 Rickard Andersson