Wrote this for a site I'm making. Thought I'd share it here. <?php //****************************** //********CONFIGURATION********* //****************************** $MSIE_URL = ""; $FIREFOX_URL = ""; $OPERA_URL = ""; $SAFARI_URL = ""; $CHROME_URL = ""; $OTHER_URL = ""; //****************************** //****************************** // Redirection code $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT']; function str_present($str,$substr) { $pos = strpos($str,$substr); if($pos === false) { return false; } else { return true; } } if (str_present($HTTP_USER_AGENT, "MSIE")) { Header ("Location: " . $MSIE_URL); } else if (str_present($HTTP_USER_AGENT, "Firefox")) { Header ("Location: " . $FIREFOX_URL); } else if (str_present($HTTP_USER_AGENT, "Chrome")) { Header ("Location: " . $CHROME_URL); } else if (str_present($HTTP_USER_AGENT, "Opera")) { Header ("Location: " . $OPERA_URL); } else if (str_present($HTTP_USER_AGENT, "Safari")) { Header ("Location: " . $SAFARI_URL); } else { Header ("Location: " . $OTHER_URL); } ?> PHP:
Thank you for your addition to this section however if i may suggest when you are planning on releasing code to the public it would be wise to have some kind of "Coding Standard and Naming Convention" as your braces looks randomly inserted and laid out.
very nice I have been debating on implementing this for some of my older sites that have issues with ie etc as id prefer users use better browsers Im currently using a old browser check that just display a big download firefox at the top of the page if ff is not detected but this would be much nicer as i can redirect ie & safari users to a custom download firefox page etc Thanks for the share
I suggest you use stripos for case insensitivity. Sometimes the user agent is not in the case as expected.
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) Is what google suggests
so i would use if (str_present($HTTP_USER_AGENT, "iPhone")) { or if (str_present($HTTP_USER_AGENT, "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) ")) { ??
Hi, this might just be me but i get Warning: Cannot modify header information - headers already sent by (output started at /home/xnalarac/public_html/drwho/index.php:1) in /home/xnalarac/public_html/drwho/index.php on line 104 do i need to put this code at the top? sorry i am answering my own question because i just tried it and you do.
Your Safari line is likely going to catch iPod, iPhone, Droid Eris, and a few other devices that use Safari as their web browser.
It won't catch iPod or iPhone users as the user agent for the iPhone is iPhone, and the one for the iPod is iPod. I guess it would be the same with the droid and the other devices
Hi, thanks for posting your work. Can I suggest the big group of IF statements might be better if re-writen as a switch statement?
Not tested but here is my version. <?php $USER_AGENT = explode('/', $_SERVER ['HTTP_USER_AGENT']); /* Eliminate IE */ if( strpos($USER_AGENT[1], 'MSIE') !== false ){ header('Locaion: ' . $MSIE_URL); } /* Redirection */ switch ($USER_AGENT[0]) { case 'Mozilla' : header('Locaion: *FIREFOX_URL*' ); break; case 'Chrome' : header('Locaion: *CHROME_URL*'); break; case 'Opera' : header('Locaion: *OPERA_URL*'); break; case 'Googlebot' : header('Locaion: *GOOGLE_BOT_URL*'); break; case 'msnbot' : header('Locaion: *MSN_BOT_URL*'); break; case 'Safari' : header('Locaion: *SAFARI_URL*'); break; default : header('Locaion: *DEFAULT_URL*'); break; } PHP:
Here's an actual iPod user-agent string, taken directly from my raw server logs for an iPod visitor. Mozilla/5.0 (iPod; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16 Code (markup): See how it has Safari towards the end there ? A simple "strpos" is going to match that user agent. Here's a user-agent for an iPhone, same situation here. Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16 Code (markup): Again, here we have an Android powered phone, again the same situation with Safari. Mozilla/5.0 (Linux; U; Android 1.5; en-us; MB200 Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1 Code (markup): What about these agents for Opera Mini ? This is a smartphone browser, but it's going to register as Opera with that script. Opera/8.01 (J2ME/MIDP; Opera Mini/3.1.10423/1724; en; U; ssr) Opera/9.80 (J2ME/MIDP; Opera Mini/5.0.18741/1114; U; id) Presto/2.4.15 Code (markup): To make things even more interesting, Apple recently approved Opera Mini for use on iPhone/iPad/etc, so now you're going to have Opera Mini user-agent strings combined with elements from iPhoneOS.
Heres the user-agent for the Opera Mini for the iPod/iPhone Opera/9.80 (iPhone; Opera Mini/5.0.0176/802; U; en) Presto/2.4.15 Code (markup):
Well spotted! I'll update the post. <?php $USER_AGENT = explode('/', $_SERVER ['HTTP_USER_AGENT']); /* Eliminate IE */ if( strpos($USER_AGENT[1], 'MSIE') !== false ){ header('Location: *IE_URL*' ); } /* Redirection */ switch ($USER_AGENT[0]) { case 'Mozilla' : header('Location: *FIREFOX_URL*' ); break; case 'Chrome' : header('Location: *CHROME_URL*'); break; case 'Opera' : header('Location: *OPERA_URL*'); break; case 'Googlebot' : header('Location: *GOOGLE_BOT_URL*'); break; case 'msnbot' : header('Location: *MSN_BOT_URL*'); break; case 'Safari' : header('Location: *SAFARI_URL*'); break; default : header('Location: *DEFAULT_URL*'); break; } Code (markup):