views:

277

answers:

4

Hello, i want to redirect peaple who goes in my site to other page if they are using a mobile browser with a php o java script. Thanks

+1  A: 

You need to check the User-Agent header:

if (preg_match("/(BlackBerry|(iP(hone|od))/i", $_SERVER['HTTP_USER_AGENT'])) ) {
    ...
}
SLaks
there is an error on the code i cant detect, can you please check it?
DomingoSL
A: 

The $_SERVER['HTTP_USER_AGENT'] contains word "BlackBerry" or "iPhone" respectively. "iPod" too if it's iPod Touch.

Timo
A: 

Very easy will be to parse USER-AGENT string or get_browser() PHP function. Try :

echo $_SERVER['HTTP_USER_AGENT'];
var_dump(get_browser(null, true));

Every browser is sending own HTTP_USER_AGENT string.

Mobile devices USER_AGENT list

For completed solution take a look to bavotasan page or just google.

retro
+1  A: 

In addition to checking the User-Agent header, you may also want to check the X-Wap-Profile and Profile headers, since some third-party browsers may not send a correct User-Agent header (they may be spoofing an IE or Firefox header). The order that I like to check the headers, when looking for mobile clients, is:

  1. X-Wap-Profile
  2. Profile
  3. User-Agent
Marc Novakowski