/    Sign up×
Community /Pin to ProfileBookmark

mobile redirect php script

Hi there,

I was hoping someone might help me edit this [URL=”http://www.pcmechanix.ca/mobile_redirect.htm”]mobile redirect code[/URL] so that it includes IPhones with the redirecting process.

Any suggestions?

FYI – Besides this minor exception, it is quite an effective code for redirecting mobile phones. You can try it for yourself, simply enter the following URL into your mobile:
[URL=”http://www.pcmechanix.ca”]http://www.pcmechanix.ca/[/URL]

Thanks,

Ash
Webmaster
[URL=”http://www.pcmechanix.ca”]PC MECHANIX – COMPUTER REPAIR SERVICES[/URL]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@Hen_AsrafMay 24.2009 — Looking at the code, this seems to work with iPhones too... Can't verify that as I have no iPhone, though.
Copy linkTweet thisAlerts:
@ashcaresauthorMay 24.2009 — I had both my buddies check it with their Iphones but it doesn't seem to redirect them?
Copy linkTweet thisAlerts:
@MindzaiMay 24.2009 — [code=php]
if(preg_match("/iphone/i",$_SERVER["HTTP_USER_AGENT"])) return false;[/code]


It doesn't redirect iphones, presumably because they can display the page 'normally' not like most phones. To have it redirect iphones change false to true.

BTW, you could make the function much more efficient by replacing all the preg_match functions with non-regex ones such stripos.
Copy linkTweet thisAlerts:
@ashcaresauthorMay 24.2009 — Thanks for your help!

Now you got me curious on making this code more efficient. Since my knowledge on this scripting is extremely basic. Will you be willing to help guide me through the process?
Copy linkTweet thisAlerts:
@MindzaiMay 24.2009 — I'd probably do something like this:

[code=php]
<?php

if(checkmobile()) header('Location:http://www.yourmobilepage.com/');

function checkmobile() {

if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) return true;
if (false !== stripos($_SERVER['HTTP_ACCEPT'], 'wap')) return true;

$reject = array(
'Creative AutoUpdate',
'MSIE',
'Trident',
'Gecko'
);

$accept = array(
'iphone',
'midp',
'j2me',
'avantg',
'docomo',
'novarra',
'palmos',
'palmsource',
'240x320',
'opwv',
'chtml',
'pda',
'windows ce',
'mmp/',
'blackberry',
'mib/',
'symbian',
'wireless',
'nokia',
'hand',
'mobi',
'phone',
'cdm',
'up.b',
'audio',
'SIE-',
'SEC-',
'samsung',
'HTC',
'mot-',
'mitsu',
'sagem',
'sony',
'alcatel',
'lg',
'erics',
'vx',
'NEC',
'philips',
'mmm',
'xx',
'panasonic',
'sharp',
'wap',
'sch',
'rover',
'pocket',
'benq',
'java',
'pt',
'pg',
'vox',
'amoi',
'bird',
'compal',
'kg',
'voda',
'sany',
'kdd',
'dbt',
'sendo',
'sgh',
'gradi',
'jb',
'moto'
);

$accept_regex = array(
'[d]{3}i'
);

if (isset($_SERVER['HTTP_USER_AGENT'])) {
// check for definite rejects
foreach ($reject as $string) {
if (false !== stripos($_SERVER['HTTP_USER_AGENT'], $string)) {
return false;
}
}
// more efficient accept check
foreach ($accept as $string) {
if (false !== stripos($_SERVER['HTTP_USER_AGENT'], $string)) {
return true;
}
}
// use regex only where necessary
foreach ($accept_regex as $string) {
if (preg_match('/'.$string.'/i', $_SERVER['HTTP_USER_AGENT'])) {
return true;
}
}
}

return false;

}

?>
[/code]
Copy linkTweet thisAlerts:
@AaliyahRomaMay 24.2009 — 301 redirects in many languages: [url=http://www.cafewebmaster.com/redirect-webpages-html-php-htaccess-javascript-cgi-perl-aspnet-and-coldfusion]http-redirects[/url]
Copy linkTweet thisAlerts:
@ashcaresauthorMay 25.2009 — First off, thank you so much for this updated script. However, in comparison to the original script it seems that this new script takes double the loading time for redirection. Do you know why that is and how I might be able to decrease loading time?

Thanks again,

Ash

Webmaster

PC Mechanix - Computer Repair Services

Serving Toronto, Markham, Richmond Hill and surrounding areas.
×

Success!

Help @ashcares spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...