Forum Moderators: coopster

Message Too Old, No Replies

page redirection

         

phex

2:47 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Hi there.

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?

ag_47

8:33 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



I'd say you check one of: $_SERVER['HTTP_HOST'] and/or $_SERVER['HTTP_USER_AGENT']

Haven't done this myself tough..

Lit_byte

1:00 am on Dec 13, 2008 (gmt 0)

10+ Year Member



One thought would be to use Javascript to detect screen resolution, then redirect based on the results. If they are accessing via a PC, then index would continue to load. If they accessed with a Phone, then index2 would load.

rainborick

4:19 am on Dec 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've only just begun to even consider mobile users since most of the sites I've dealt with in the past are simply inappropriate for viewing on anything like a cell phone or PDA of any kind. So keep in mind that I have no direct experience and I'm just hoping to add to the discussion so I can learn, too. Still, my first thought would be to segregate all of your pages for mobile users into its own directory within your site to simplify issues for the redirect code and for search engines.

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.

Mahabub

5:41 pm on Dec 14, 2008 (gmt 0)

10+ Year Member



dear phex,

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
}