Mobile Browser Identification

Ask about a PHP problem here.
Locked
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Mobile Browser Identification

Post by ta2shop »

one question! how can i combine this to lines so thei wil work togeder?
for the browser identity, i have a functions.php file witch has the browser name like so:
$pre = $_SERVER['HTTP_USER_AGENT'];
  
if(strpos($pre, "Firefox")!=0)
$navegador = "Firefox";
 
else if(strpos($pre, "AppleWebKit/533.19.4")!=0)
$navegador = "Safari";
 
else if(strpos($pre, "AppleWebKit/534.24")!=0)
$navegador = "Chrome"; 

else if(strpos($pre, "MSIE 9.0")!=0)
$navegador = "Internet Explorer 9";
 
else if(strpos($pre, "MSIE 8.0")!=0)
$navegador = "Internet Explorer 8";

else if(strpos($pre, "MIDP-2.1")!=0)
$navegador = "BlackBerry 8520";

else if(strpos($pre, "AppleWebKit/530.17")!=0)
$navegador = "Samsung Galaxy 3";

else if(strpos($pre, "AppleWebKit/533.1")!=0)
$navegador = "HTC Desire"; 

else if(strpos($pre, "MIDP-2.0")!=0)
$navegador = "Samsung GT-S3550"; 

else
$navegador = "Unknown";
and the index.php file witch displayes everithing like so:
$pre = $_SERVER['HTTP_USER_AGENT'];

if(strpos($pre, "Firefox")!=0)
 echo"<img src='img/firefox.jpg' width='250' height='250' />";
  
else if(strpos($pre, "AppleWebKit/534.24")!=0)
	echo "<img src='img/chrome.jpg' width='250' height='250' />";
	
else if(strpos($pre, "AppleWebKit/533.19.4")!=0)
	echo "<img src='img/safari.jpg' width='250' height='250' />";
	
else if(strpos($pre, "MIDP-2.1")!=0)
	echo "<img src='img/blackberry.JPG' width='150' height='150' />";
	
else if(strpos($pre, "MSIE 8.0")!=0)
	echo "<img src='img/ie.jpg' width='250' height='250' />";
	
else if(strpos($pre, "MSIE 9.0")!=0)
	echo "<img src='img/ie.jpg' width='250' height='250' />";
	
else if(strpos($pre, "AppleWebKit/530.17")!=0)
	echo "<img src='img/samsung.jpg' width='150' height='150' />";
	
else if(strpos($pre, "AppleWebKit/533.1")!=0)
	echo "<img src='img/htc.jpg' width='150' height='150' />";
	
else if(strpos($pre, "MIDP-2.0")!=0)
	echo "<img src='img/samsung.jpg' width='150' height='150' />";
	

else
	echo"Unknown";
how could i get the same functionality but in one file?
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Mobile Browser Identification

Post by jacek »

Not 100% sure what you mean, but you could define the $navigator variable where you output the img tag
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Mobile Browser Identification

Post by nyo »

Can't you put the echos into the function.php file like this:
if(strpos($pre, "Firefox")!=0)
{
   $navegador = "Firefox";
   echo "<img src='img/firefox.jpg' width='250' height='250' />";
}
Or did you ask something else?
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Mobile Browser Identification

Post by ta2shop »

yeh i alrady changed this, it is much more complex now! just forgot to update the code in the first post.
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Mobile Browser Identification

Post by Tino »

Printing output within a function is generally not considered a best practice. It's better to return the output instead, then 'echo' the function.
function do_something() {
    return "I'm doing stuff, wooo!";
}

echo do_something();
Last edited by jacek on Sun May 15, 2011 8:57 pm, edited 1 time in total.
Reason: added missing "not"
Please check out my CodeCanyon items.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Mobile Browser Identification

Post by nyo »

Tino wrote:Printing output within a function is generally not considered a best practice. It's better to return the output instead, then 'echo' the function.
Tino, I guess it is not a function, it is just in functions.php.

ta2shop, maybe we didn't understand what you really want to accomplish...
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Mobile Browser Identification

Post by ta2shop »

nailyener wrote:
Tino wrote:Printing output within a function is generally not considered a best practice. It's better to return the output instead, then 'echo' the function.
Tino, I guess it is not a function, it is just in functions.php.

ta2shop, maybe we didn't understand what you really want to accomplish...
it is a browser identification, what did you not understand?
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Mobile Browser Identification

Post by nyo »

ta2shop wrote:it is a browser identification, what did you not understand?
I know it is a browser identification, what I did not understand is your last post:
ta2shop wrote:yeh i alrady changed this, it is much more complex now! just forgot to update the code in the first post.
So, everything is ok now?
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Mobile Browser Identification

Post by ta2shop »

nailyener wrote:
ta2shop wrote:it is a browser identification, what did you not understand?
I know it is a browser identification, what I did not understand is your last post:
ta2shop wrote:yeh i alrady changed this, it is much more complex now! just forgot to update the code in the first post.
So, everything is ok now?
this last post is totaly irelevant, i already said that the problem is fixed! forget about this post, focus on the next one!
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Mobile Browser Identification

Post by jacek »

ta2shop wrote:this last post is totaly irelevant, i already said that the problem is fixed! forget about this post, focus on the next one!
Since this is apparently just people being confused, locked.

PM me if you want it un-locked :)
Image
Locked