Home | Forums | Mark forums read | Search | FAQ | Login

Advanced search
Hot Topics
Buraku hot topic Iran, DPRK, Nuke em, Like Japan
Buraku hot topic Re: Adam and Joe
Buraku hot topic Multiculturalism on the rise?
Buraku hot topic Homer enters the Ghibli Dimension
Buraku hot topic MARS...Let's Go!
Buraku hot topic Saying "Hai" to Halal
Buraku hot topic Japanese Can't Handle Being Fucked In Paris
Buraku hot topic Russia to sell the Northern Islands to Japan?
Buraku hot topic 'Oh my gods! They killed ASIMO!'
Buraku hot topic Microsoft AI wants to fuck her daddy
Change font size
  • fuckedgaijin ‹ General ‹ Tokyo Tech ‹ Computers & Internet

Smartphone detection for Websites

Hardware, Software, Internet, Networking, Programmming, Web Design, Linux, OS X, Windows, etc. News, disucssion and support.
Post a reply
4 posts • Page 1 of 1

Smartphone detection for Websites

Postby IparryU » Mon Oct 29, 2012 5:19 pm

A friend of mine is trying to get a website to detect if the visitor has a smartphone/iphone and rediret them to the mobile page.

PHP is being used now, but no detection has worked thus far. I took a look at it (i dont have any knowleged of PHP) to see if I can spot some conditional error, but I dont know...

Approach 1 is used as a call function in the index.html file whils Approach 2 is index.php.

any help would be kick ass.

Approach 1:
Code: Select all
<?php
function DetectBrowser() {
    $mobileBrowser= false; // set mobile browser as false till we can prove otherwise
   $sumaho= false;
   $keitai= false;
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
   $newPage= "http://www.kitagawa-hirataka.com/s";
   $oldPage= "http://www.kitagawa-hirataka.com/m";
   $mainPage= "http://www.kitagawa-hirataka.com/m";

   if(eregi("DoCoMo",$user_agent)){$keitai=true; }
   if(eregi("UP\.Browser",$user_agent)){$keitai=true; }
   if(eregi("J-PHONE",$user_agent)){$keitai=true; }
   if(eregi("Vodafone",$user_agent)){$keitai=true; }   
   if(eregi("SoftBank",$user_agent)){$keitai=true; }   
   if(eregi("J-EMULATOR",$user_agent)){$keitai=true; }   
   
    if (eregi('ipod',$user_agent)||eregi('iphone',$user_agent)) {$sumaho=true; }//end iphone check
    if (eregi('opera mini',$user_agent)) {$sumaho=true; }//end opera check
    if (eregi('android',$user_agent)) {$sumaho=true; }//end android check
   
    if ($sumaho) {header('Location: '.$newPage); } //end smartphone check redirect
   if ($keitai) {header('Location: '.$oldPage); } //end keitai check redirect
   if (($keitai=false) && ($sumaho=false)) {header('Location: '.$mainPage); } //end keitai check redirect
}//end detect browser function
?>


Approach 2
Code: Select all
$docomo = "./m/index.html";      //ƒhƒRƒ‚‚̏ꍇ
$au = "./m/index.html";         //‚‚•‚̏ꍇ
$softbank = "./m/index.html";   //ƒ\ƒtƒgƒoƒ“ƒN‚̏ꍇ
$msie = "top.html";         //‚h‚d‚̏ꍇ
$Netscape = "top.html";      //Netscape‚̏ꍇ
$opera = "top.html";         //Opera‚̏ꍇ
$firefox = "top.html";      //Firefox‚̏ꍇ
$pc = "top.html";         //ã‹L‹@ŽíˆÈŠO
/////////////////////////////////////////£‰Šúˆ—I’[£
/////////////////////////////////////////¥Œg‘Ñ”»•ʁ¥
if(isset($_SERVER['HTTP_USER_AGENT'])){
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if(eregi("DoCoMo",$user_agent)){header("Location: $docomo");}
elseif(eregi("UP\.Browser",$user_agent)){header("Location: $au");}
elseif(eregi("J-PHONE",$user_agent)){header("Location: $softbank");}
elseif(eregi("Vodafone",$user_agent)){header("Location: $softbank");}
elseif(eregi("SoftBank",$user_agent)){header("Location: $softbank");}
elseif(eregi("J-EMULATOR",$user_agent)){header("Location: $softbank");}
elseif(eregi("MSIE",$user_agent)){header("Location: $msie");}
elseif(eregi("Netscape",$user_agent)){header("Location: $netscape");}
elseif(eregi("Opera",$user_agent)){header("Location: $opera");}
elseif(eregi("Firefox",$user_agent)){header("Location: $firefox");}
else{header("Location: $pc");}
}else{
print <<<END
<html><body>
HTTP_USER_AGENT Error<br /><br />
ƒ†[ƒU[ƒG[ƒWƒFƒ“ƒg‚ª“ǂݍž‚߂܂¹‚ñ‚Å‚µ‚½B<br />
</body></html>
END;
}
User avatar
IparryU
Maezumo
 
Posts: 4285
Joined: Thu Jun 04, 2009 11:09 pm
Location: Balls deep draining out
Top

Re: Smartphone detection for Websites

Postby Coligny » Mon Oct 29, 2012 5:42 pm

Don't the old user agent tricks developped against internoot exploder works against mobiles ?
User avatar
Coligny
 
Posts: 21818
Images: 10
Joined: Sat Jan 17, 2009 8:12 pm
Location: Mostly big mouth and bad ideas...
  • Website
  • Personal album
Top

Re: Smartphone detection for Websites

Postby yanpa » Mon Oct 29, 2012 6:04 pm

Aha, a classic case of assignment vs. comparison:
Code: Select all
if (($keitai=false) && ($sumaho=false))

Should be:

Code: Select all
if (($keitai===false) && ($sumaho===false))

(Yes, 3 "=" characters, this is PHP...)

The original if-clause will always evaluate to TRUE, no matter what $keitai and $sumaho were set to, so the $mainPage redirect will always be sent.

Correction, the original if-clause will always evaluate to FALSE, so the $mainPage redirect will never be sent. I.e. that if the user agent is not detected as a mobile/smartphone, no redirect will be sent.

Dunno whether that's the cause of the problem, mind...

Also, it might be worth suggesting to your friend to take a gander at the nifty "else" keyword :idea:
User avatar
yanpa
 
Posts: 5671
Images: 11
Joined: Sun Nov 04, 2007 11:50 am
Location: Tokyo
Top

Re: Smartphone detection for Websites

Postby cstaylor » Mon Oct 29, 2012 6:17 pm

Better idea: don't reinvent the wheel.

PHP Mobile Detect
User avatar
cstaylor
 
Posts: 6383
Joined: Mon Apr 29, 2002 2:07 am
Location: Yokohama, Japan
  • Website
Top


Post a reply
4 posts • Page 1 of 1

Return to Computers & Internet

Who is online

Users browsing this forum: No registered users and 0 guests

  • Board index
  • The team • Delete all board cookies • All times are UTC + 9 hours
Powered by phpBB® Forum Software © phpBB Group