Posted by dj001 3 years 3 weeks ago
New to this, please bear with me.
I want to put some code at the top of my regular homepage redirecting MOBILE ONLY visitors to my mobile home page, which is m.htm.
Here is the top of my regular homepage:
<html> <head> <link rel="shortcut icon" href="favicon.ico" > <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <meta name="GENERATOR" content="Microsoft FrontPage 6.0"> <meta name="ProgId" content="FrontPage.Editor.Document">
Here is the code I got from your website, with a redirect command at the bottom:
<?php
$mobile_browser = '0';
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$mobile_browser++;
}
if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$mobile_browser++;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','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','oper','palm','pana','pant','phil','play','port','prox',
'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','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-');
if(in_array($mobile_ua,$mobile_agents)) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
$mobile_browser=0;
}
if($mobile_browser>0) {
<meta HTTP-EQUIV="REFRESH" CONTENT="0;url=m.htm">
}
else {
// do something else
}
?>I uploaded the file by itself and tested it on my PC. It redirected me to the mobile site, so something is not working right. Please tell me how to fix it and EXACTLY WHERE to put the code on my regular homepage.




Posted by MRLacey 3 years ago
if($mobile_browser>0) { header("Location: http://m.yoursite.com/"); /* Redirect browser */ exit; /* Make sure that code below does not get executed after redirect. */ }Posted by dj001 3 years ago
So, based on your suggestion, I rewrote it as follows:
<?php $mobile_browser = '0'; if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { $mobile_browser++; } if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { $mobile_browser++; } $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','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','oper','palm','pana','pant','phil','play','port','prox', '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','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda','xda-'); if(in_array($mobile_ua,$mobile_agents)) { $mobile_browser++; } if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) { $mobile_browser++; } if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) { $mobile_browser=0; } else { if($mobile_browser>0) { header("Location: http://mysite.com/m.htm/"); /* Redirect browser */ exit; /* Make sure that code below does not get executed after redirect. */ } if($mobile_browser>0) { <meta HTTP-EQUIV="REFRESH" CONTENT="0;url=m.htm"> } ?>When I navigate to the page on a PC, it still redirects me to the mobile site. I must be doing something wrong.
Also, EXACTLY WHERE do I put the code in my homepage? Here is the code:
Posted by eklund 3 years ago
You can put that code at the top of your page, before your HTML, and save as
your-filename.phpPosted by dj001 3 years ago
I saved the file as .htm. So, I should save it as a .php instead? If I want to redirect mobile visitors only to my regular homepage to my mobile page, shouldn't there be something in my regular homepage that refers to the .php file?
Posted by eklund 3 years ago
Assuming your server is set up to execute php files!
I suggest you make the php page be your homepage, and have your normal homepage content after then end of the php code above.
I would also remove the following:
if($mobile_browser>0) { <meta HTTP-EQUIV="REFRESH" CONTENT="0;url=m.htm"> }Posted by dj001 3 years ago
Now, I get this error:
Parse error: syntax error, unexpected $end in /home/www/mysite.com/m3.php on line 44
Here is the code:
<?php $mobile_browser = '0'; if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { $mobile_browser++; } if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { $mobile_browser++; } $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','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','oper','palm','pana','pant','phil','play','port','prox', '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','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda','xda-'); if(in_array($mobile_ua,$mobile_agents)) { $mobile_browser++; } if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) { $mobile_browser++; } if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) { $mobile_browser=0; } else { if($mobile_browser>0) { header("Location: http://mysite.com/m.htm/"); /* Redirect browser */ exit; /* Make sure that code below does not get executed after redirect. */ } ?>Posted by eklund 3 years ago
Looks like you need one more closing curly bracket before the last
?>Posted by dj001 3 years ago
OK, that gets rid of the parsing error. But the file opens a blank page both on a PC and on my HTC 8525 mobile.
Posted by ruadhan 3 years ago
Mobile Champion
Can you post your entire page, PHP + HTML?
Ruadhan O'DonoghuedotMobi
Posted by dj001 3 years ago
php file:
<?php $mobile_browser = '0'; if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { $mobile_browser++; } if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { $mobile_browser++; } $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','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','oper','palm','pana','pant','phil','play','port','prox', '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','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda','xda-'); if(in_array($mobile_ua,$mobile_agents)) { $mobile_browser++; } if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) { $mobile_browser++; } if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) { $mobile_browser=0; } else { if($mobile_browser>0) { header("Location: http://mysite.com/m.htm/"); /* Redirect browser */ exit; /* Make sure that code below does not get executed after redirect. */ } } ?>header of index.htm:
Posted by xyz 3 years ago
Hello,
is this header mandatory?
Thank you.
Posted by dj001 3 years ago
Yes, it is.
My homepage is index.htm and is part of a FrontPage navigation. If I copy index.htm, rename the copy main.htm, delete index.htm, and rename the redirect file index.php, will visitors to mysite.com (there are millions) be routed properly?
Posted by ruadhan 3 years ago
Mobile Champion
You will probably need to configure IIS to set index.php as the default file to be loaded when visitors hit your site. You can do this in the IIS snap-in tool in the Administrative Tools of you server.
Ruadhan O'DonoghuedotMobi
Posted by microgers206 3 years ago
is there an else statement that i can put in there to send to the PC page? or do i have to make the index a php file?
Posted by johnwircken 3 years ago
This comment has been moved here.
Posted by plavalle 2 years ago
DJ001-
You need two files. One that does the check for mobile and redirect, the other for the actual content.
The problem lies here:
if($mobile_browser>0) { header("Location: http://m.yoursite.com/m.htm"); /* Redirect browser */ exit; /* Make sure that code below does not get executed after redirect. */ }Your page has already loaded with content and the header info can not be scrubbed and redirect.
php content must run on a page with the extension .php, NOT .htm
create a page index.php and include this:
<?php $mobile_browser = '0'; if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { $mobile_browser++; } if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { $mobile_browser++; } $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','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','oper','palm','pana','pant','phil','play','port','prox', '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','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda','xda-'); if(in_array($mobile_ua,$mobile_agents)) { $mobile_browser++; } if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) { $mobile_browser++; } if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) { $mobile_browser=0; } else { if($mobile_browser>0) { header("Location: http://m.yoursite.com/m.htm"); /* Redirect browser */ exit; /* Make sure that code below does not get executed after redirect. */ } } ?>next create a page called m.htm (referenced in the index.php code above)
The blank page your getting is due to the header refresh with content on the same page as the redirect...
Let us know if you have success now.
Posted by JoblessPunk 2 years ago
@plavalle
Thank you so much for this, I know you were helping someone else but it helped me out and works like a charm.
Thanks!
Posted by grimmylina 2 years ago
Yes, thank you Plavalle!!!! That works..I am so happy.
Posted by JoblessPunk 2 years ago
Hello,
So I just did some field testing with this and I only had about half of the phones actually redirect.
Here are the phones that did not redirect:
T-Mobile - MyTouch
G1
Sidekick (all)
Motorola DROID
Pixie (all)
Garmin
Sprint Interpid
HTC Snap
HTC Touch Pro
Is there a way to add these phones to the list to redirect them to the mobile site?
Thank you,
tim
Posted by vampas 2 years ago
SO, SO, SO MANY THANKS..... it helped me so.....
Posted by lzbthmartini 1 year ago
Your script is great and has helped me out a lot, Thanks!
But recently, I've started seeing getting this message:
Notice: Undefined index: ALL_HTTP'for this statement:
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) { $mobile_browser++; }I think it's only appeared recently. I sit something I should worry about?
thanks,
-Elizabeth
Posted by wolf5659 27 weeks ago
Sup guys I am a total noob trying to figure this out...
So this is what I did, in the main folder on my server I had an index.php that has the long script above.
1-do I follow this script with my website elements? or just that script alone in the index.php?
2-I I created another file called index.html within a different folder and made my php redirect to it.
My main website is a flash website, The other website I want to redirect to is my mobile version.
When I log on I still get the flash website "which of course doesn't run on iphone or ipad"
can someone explain this in a step by step please?
Posted by xiaopy12 1 week ago
Christian Louboutin Sale sdrfyhey
Posted by xiaopy12 1 week ago
Ralph Lauren Outlet dfhth