I'm trying to identify a visitor's phone type based on their UserAgent string. I can't find the error in my code, however.
<?php
//create an array to be filled with phone type
$arr = phones();
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
$nokia = strpos($_SERVER['HTTP_USER_AGENT'],"Nokia");
if ($iphone || $android || $palmpre || $ipod || $berry || $nokia == true)
{
echo "Specially for you as $iphone owner!";
}
?>
Taking into account the other comments, you shouldn't do this yourself. These user agents are nowhere near an exhaustive list. But if you'd like to experiment yourself I'd recommend trying out something like the following.
Also if "iPhone"
is not found then the condition is automatically false and no other statements will be evaluated.
I like doing something like:
$has_mobile_ua = FALSE;
$user_agents = array('iPhone', 'Android', 'webOS', 'BlackBerry', 'iPod', 'Nokia');
foreach ($user_agents as $user_agent) {
if (strpos($_SERVER['HTTP_USER_AGENT'], $user_agent) !== FALSE) {
$has_mobile_ua = TRUE;
break;
}
}
if ($has_mobile_ua) {
// your code
}
for this case, try change strpos with stristr..
$iphone = stristr("$_SERVER[HTTP_USER_AGENT]","iphone");
it's work for me, with same code like yours..