Forum Moderators: coopster
I want my website to be accessed by both “pc and cell phones browsers”. Since the monitor sizes of both pc’s and cell phones are not equal, I have developed two index pages.
1)index.php for pc browsers
2)index2.php for cell phone browsers.
Both pages are XHTML compliant.
What I want is that when a visitor enters eg mysite.com from his cell phone browser, he should be automatically redirected to index2.php.
How do I do that using PHP?
My first instinct was to suggest looking into using a special stylesheet for mobile users since it would let the browser take care of the crucial decision. I don't know about anyone else, but it would require a major redesign for any site I've built to elegantly realign itself based solely on a dedicated stylesheet. This method is really only good if your pages can be restructured decently via the stylesheet. The only thing I could think of to take advantage of this method on a conventional page would be to add a DIV that would only be visible on mobile devices and have it contain a link to your index2.php page for mobile users to click on. Ugly, I know.
So, it comes back to sniffing the User Agent ($_SERVER['HTTP_USER_AGENT'] as ag_47 noted above) and trying to determine if its a conventional browser or a mobile device. I did a quick search on 'user agent mobile' and found several lists of User Agents for mobile devices. Turns out there's a large number, but you should be able to construct a set of regular expressions to detect them broadly and do the redirects as needed. I think it would be better to do it at the server level via .htaccess on Apache servers or whatever tools IIs allows you since you'd be able to handle the issue on a sitewide basis, rather than page-by-page. Doing a redirect with PHP, of course, requires taking care that the redirect happens before any other output. With so many handsets being able to handle conventional web pages now, you'll want to consider which of them truly need to be redirected. So try looking up mobile user agents and work out the regexp's to detect them. That will get you started.
Thinks the below functions will help you...
function detect_mobile_device(){
// check if the user agent value claims to be windows but not windows mobile
if(stristr($_SERVER['HTTP_USER_AGENT'],'windows')&&!stristr($_SERVER['HTTP_USER_AGENT'],'windows ce')){
return false;
}
// check if the user agent gives away any tell tale signs it's a mobile browser
if(eregi('up.browser¦up.link¦windows ce¦iemobile¦mini¦mmp¦symbian¦midp¦wap¦phone¦pocket¦mobile¦pda¦psp',$_SERVER['HTTP_USER_AGENT'])){
return true;
}
// check the http accept header to see if wap.wml or wap.xhtml support is claimed
if(stristr($_SERVER['HTTP_ACCEPT'],'text/vnd.wap.wml')¦¦stristr($_SERVER['HTTP_ACCEPT'],'application/vnd.wap.xhtml+xml')){
return true;
}
// check if there are any tell tales signs it's a mobile device from the _server headers
if(isset($_SERVER['HTTP_X_WAP_PROFILE'])¦¦isset($_SERVER['HTTP_PROFILE'])¦¦isset($_SERVER['X-OperaMini-Features'])¦¦isset($_SERVER['UA-pixels'])){
return true;
}
// build an array with the first four characters from the most common mobile user agents
$a = array('acs-','alav','alca','amoi','audi','aste','avan','benq','bird','blac','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','newt','noki','opwv','palm','pana','pant','pdxg','phil','play','pluc','port','prox','qtek','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','tosh','tsm-','upg1','upsi','vk-v','voda','w3c ','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-');
// check if the first four characters of the current user agent are set as a key in the array
if(isset($a[substr($_SERVER['HTTP_USER_AGENT'],0,4)])){
return true;
}
}
if(detect_mobile_device()){
// redirect to index2.php
}
else {
// redirect to index.php or execute the index.php file
}