Mobile Browser Redirect

PHP in HTML to redirect mobile browsers

Page 1 of 1

14 Replies - 29605 Views - Last Post: 01 July 2011 - 05:55 PM Rate Topic: -----

#1 jbrill1012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 09

Mobile Browser Redirect

Post icon  Posted 03 March 2009 - 06:37 AM

Okay first of I've found a lot of information on redirection of a web site the problem is it's to much I can't seem to find what I need and for some reason I can't get this to work properly. I welcome suggestions as well as help. What I am trying to do is basically go through and at the top of a pretty basic html page insert php to detect if the page is being visited by a mobile browser and if so redirect to a particular page that is more designed for a mobile device. I would even be happy with just 1 USER_AGENT of "BlackBerry" and if any other browser display as normal. Here is the code that I found trying to learn how to do this on my own:

<?php
/*
=====================================================
Mobile version detection
-----------------------------------------------------
compliments of http://www.buchfelder.biz/
=====================================================
*/
for ($i=0;$i<$result;$i++)
{$ausg = stristr($text,$var[$i]);if(strlen($ausg)>0){header("location: $mobile");exit;}}
$text = $_SERVER['HTTP_USER_AGENT'];
$var[0] = 'Mozilla/4.';
$var[29] = 'BlackBerry';
$mobile = "http://www.mc4bb.com/mobile.htm";
$result = count($var);?>



There are several other lines of code that I excluded so as to not kill the thread each variable has it's own line and number. Where the problem I'm seeing is seemingly a simple problem I can't figure out where it is. MSIE user agent and BlackBerry along with mobile browsers can't access the page. I keep getting a header error "Warning: Cannot modify header information-headers already sent by (output started at /home/content/j/b/r/myaccountname/html/index.php:10) in /home/content/j/b/r/myaccountname/html/index.php on line 89"

Line 10 being the start of the PHP and line 89 being where the header("location: $mobile") was before I moved it.

I removed all white spaces and that seemed to fix the error for FireFox only. I moved the 2 lines starting with "for ( ending with }}" from the bottom of the php to the top of the php and it fixed the error but it is not redirecting. The original code can be found at http://www.stepforth...-redirects.html as a link to a text file. Full code that has had all spaces removed and the original code that has had the spaces removed can be found at http://www.mc4bb.com/phpredirect.txt

It's taken me 2 days of searching to actually find this site and it seems very comprehensive hence why I signed up here instead of the other 50 sites I've visited and searched over the past 2 days. I searched the site here and I can't seem to find a better solution than posting a new topic as most of the search topics deal with totally different subjects dealing with password and db redirecting not just a very simple user_agent check and redirect. I say simple but considering I can't seem to get it to click in my head how to do it on my own it's not so simple for me.

I have a plugin for Firefox that allows me to change the USER_AGENT to what ever I want. I have this page loaded on a local server as well as a live server it works fine on local but not live, which the emulation for dreamweaver is safari so I know it works for that browser as well.

Live server is running PHP5 and I have both

AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm

loaded in my htaccess file.

Is This A Good Question/Topic? 0
  • +

Replies To: Mobile Browser Redirect

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Mobile Browser Redirect

Posted 03 March 2009 - 09:02 AM

Bad idea.

Redirect my iPhone to a mobile version of your site and I'll probably never visit again.
Was This Post Helpful? 1
  • +
  • -

#3 cbrickhouse   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 01-March 09

Re: Mobile Browser Redirect

Posted 03 March 2009 - 09:21 AM

i actually use wurfl.xml to see if a browser is mobile or not.

http://wurfl.sourceforge.net/

i made a coldfusion script that downloads and parses it once a week. here's the code if you can convert it to php.

<cftry>
	<cffile action="delete" file="c:\web\stereokiller\www\wurfl.xml">
	<cfhttp method="get" url='http://wurfl.sourceforge.net/wurfl.xml' resolveurl="false" timeout="10" path="c:\web\stereokiller\www\" file="wurfl.xml">
	</cfhttp>
	<cfcatch type="any">
		<cfset err=1>
		<cfmail to="[email protected]" subject="wurfl failed" from="[email protected]">
		wurfl failed. re-run.
		</cfmail>
	</cfcatch>
</cftry>
<cfif not isdefined('err')>
	<cfstoredproc procedure="spClearTable" datasource="#appDataSource#" username="#appSQLUserName#" password="#appSQLPassword#">
		<cfprocresult name="clrTable">
	</cfstoredproc>
	<cfscript>
	   myxmldoc = XmlParse("d:\web\stereokiller\www\wurfl.xml");
	   selectedElements = XmlSearch(myxmldoc, "wurfl/devices/device");
	   writeoutput(ArrayLen(selectedElements));	   
	</cfscript>
	<cfloop from="1" to="#arraylen(selectedelements)#" index='i'>
		<!---
		<cfif not findnocase("generic_", trim(selectedElements[i].XmlAttributes.fall_back))>
		--->
		<cftry>
		<cfif isXmlNode(selectedElements[i].xmlchildren[1]) and trim(selectedElements[i].XmlAttributes.id) neq "generic_web_browser" and len(trim(selectedElements[i].XmlAttributes.user_agent))>
			<cfquery  datasource="#appDataSource#" username="#appSQLUserName#" password="#appSQLPassword#">
				insert into tblBrowser (txtBrowsername) values ('#selectedElements[i].XmlAttributes.user_agent#')
			</cfquery>
		</cfif>
		<cfcatch type="any">
			<cfoutput>#cfcatch.Detail#</cfoutput><br>
		</cfcatch>
		</cftry>
	</cfloop>
</cfif>



then i just have a stored procedure that i pass the http user agent

ALTER PROCEDURE [dbo].[spCheckMobile]

	@user_agent varchar(200)
as
BEGIN
	select intbrowserid,txtbrowsernaME from tblBrowser where @user_agent like txtbrowsername + '%'
END



if a recordcount is returned, the browser is mobile, and i personally give the person the choice if they want to use the mobile site or the regular site.

only issue with this is, google comes across as a mobile browser for some reason.

This post has been edited by cbrickhouse: 03 March 2009 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 jbrill1012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 09

Re: Mobile Browser Redirect

Posted 03 March 2009 - 10:03 AM

View PostCTphpnwb, on 3 Mar, 2009 - 08:02 AM, said:

Bad idea.

Redirect my iPhone to a mobile version of your site and I'll probably never visit again.



Sorry not a bad idea when a BlackBerry by default doesn't support java nor css. The person actually has to go in and turn the features on. This device is my only target redirect any other device I'm not worried about. Most people using one of these devices don't have any idea that their device can do much more than what it does. So by building a site directly supporting the default features will not only enhance their experience but will allow a more dynamic site to be done, but I thought I made it clear when I stated

jbrill1012, on 3 Mar, 2009, said:

I would even be happy with just 1 USER_AGENT of "BlackBerry" and if any other browser display as normal.
and also cut out the complete code but the part needed to actually see a possible error. Thank you for your input though you're what 1 of how many iPhones sold?

I've come to the conclusion that I can use a simple if and else statement without the use of all the arrays.

This is working localhost so I'll upload it and see if it works correctly on the live server.
<?php
$main="index.htm";
$mobile="http://www.mc4bb.com/mobile.htm";
if($_SERVER['USER_AGENT']=='BlackBerry')
{
header("location: $mobile");
}
else
{
header("location: $main");
}
?> 


EDIT:

Okay the code is there and no it's not working I'm back to the point of it not redirecting correctly. I know the example of 'BlackBerry' is correct due to the listing by BlackBerry development pdf file they provided states in pearl:
$content = $ENV{'HTTP_ACCEPT'};
$browser = $ENV{'HTTP_USER_AGENT'};
if ($content =~ html) {
if ($browser =~ BlackBerry) {
print "Location: http://mobile.blackberry.com/index.html", "\n\n";
}


Added HTTP_ to the first part of the user_agent to see if that will change anything.

This post has been edited by jbrill1012: 03 March 2009 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#5 cbrickhouse   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 01-March 09

Re: Mobile Browser Redirect

Posted 03 March 2009 - 10:16 AM

i'll stick with the notion that you should use the WURFL xml file to determine a mobile browser.
Was This Post Helpful? 0
  • +
  • -

#6 jbrill1012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 09

Re: Mobile Browser Redirect

Posted 03 March 2009 - 10:42 AM

View Postcbrickhouse, on 3 Mar, 2009 - 08:21 AM, said:

i actually use wurfl.xml to see if a browser is mobile or not.

http://wurfl.sourceforge.net/

i made a coldfusion script that downloads and parses it once a week. here's the code if you can convert it to php.

if a recordcount is returned, the browser is mobile, and i personally give the person the choice if they want to use the mobile site or the regular site.

only issue with this is, google comes across as a mobile browser for some reason.


I do appreciate the suggestion. My problem with this is 1. I'm trying to learn PHP 2. You used a lot more code than what I need to accomplish with 1 browser type redirect.

Just trying to figure out what kind of problem I'm having. I got rid of the error by just putting the last part of that code I posted into just 1 index.php file nothing else in the file but it is still having a problem with a correct redirect.

It picks up the redirect to index1.htm but will not pick the 'BlackBerry' up to redirect it back to the mobile.htm
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Mobile Browser Redirect

Posted 03 March 2009 - 11:46 AM

View Postjbrill1012, on 3 Mar, 2009 - 12:03 PM, said:

<?php
$main="index.htm";
$mobile="http://www.mc4bb.com/mobile.htm";
if($_SERVER['USER_AGENT']=='BlackBerry')
{
header("location: $mobile");
}
else
{
header("location: $main");
}
?> 


EDIT:

Okay the code is there and no it's not working I'm back to the point of it not redirecting correctly. I know the example of 'BlackBerry' is correct...

I suggest that you echo the results from $_SERVER['USER_AGENT'] since I doubt it will ever simply be "BlackBerry".

As for me being one out of tens of millions of iPhone users, I'm not alone. A simple Google search will show you that users don't like to be redirected to mobile sites. I doubt BlackBerry users are different in that respect, but then again, maybe BlackBerry browsing isn't as good as Safari on the iPhone.
Was This Post Helpful? 1
  • +
  • -

#8 jbrill1012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 09

Re: Mobile Browser Redirect

Posted 03 March 2009 - 04:26 PM

View PostCTphpnwb, on 3 Mar, 2009 - 10:46 AM, said:

View Postjbrill1012, on 3 Mar, 2009 - 12:03 PM, said:

<?php
$main="index.htm";
$mobile="http://www.mc4bb.com/mobile.htm";
if($_SERVER['USER_AGENT']=='BlackBerry')
{
header("location: $mobile");
}
else
{
header("location: $main");
}
?> 


EDIT:

Okay the code is there and no it's not working I'm back to the point of it not redirecting correctly. I know the example of 'BlackBerry' is correct...

I suggest that you echo the results from $_SERVER['USER_AGENT'] since I doubt it will ever simply be "BlackBerry".

As for me being one out of tens of millions of iPhone users, I'm not alone. A simple Google search will show you that users don't like to be redirected to mobile sites. I doubt BlackBerry users are different in that respect, but then again, maybe BlackBerry browsing isn't as good as Safari on the iPhone.



It was a joke doesn't matter to me which phone you have/want. I'm just trying to get this fixed. When php looks at a USER_AGENT string does it matter if there is a space before or after? I'll be back in a little while to work on it some more, but I know 'BlackBerry' is the first word in their string for every single model so I'll work with it a bit and see what I can come up with.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Mobile Browser Redirect

Posted 03 March 2009 - 04:58 PM

"BlackBerry" != "BlackBerry ... Something else"
Was This Post Helpful? 0
  • +
  • -

#10 Valek   User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Mobile Browser Redirect

Posted 03 March 2009 - 06:19 PM

Because of what CT has said above, you should consider using Regular Expressions.

This post has been edited by Valek: 03 March 2009 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

#11 jbrill1012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 09

Re: Mobile Browser Redirect

Posted 03 March 2009 - 08:35 PM

View PostCTphpnwb, on 3 Mar, 2009 - 03:58 PM, said:

"BlackBerry" != "BlackBerry ... Something else"


If I did do it the way you're suggesting or the way at least I'm reading it is you're matching it against another string giving it a true statement, but here lies the problem of the incoming string.

The string sent from BlackBerry is BlackBerrymodel/software version then the rest of the string

Example: BlackBerry8330/4.5.0.131

Since each string contains the word BlackBerry followed by the model # there shouldn't be a problem detecting it.

Here is BlackBerry's example code that's in Perl since I don't know Perl and I'm trying to get a grasp on 1 language to begin with I'd prefer to not trying to cross boundries I'm not ready for just yet.
# Copyright (C) 2004 Research In Motion Limited.
# Note: URLs are used in this example for non-existent web sites.
#!c:\perl\bin\
$content = $ENV{'HTTP_ACCEPT'};
$browser = $ENV{'HTTP_USER_AGENT'};
if ($content =~ html) {
if ($browser =~ BlackBerry) {
print "Location: http://mobile.blackberry.com/index.html", "\n\n";
}
elsif ($browser =~ Mozilla) {
print "Location: http://www.blackberry.com/index.shtml", "\n\n";
}
}
elsif ($content =~ wml) {
if ($browser =~ BlackBerry) {
print "Location: http://mobile.blackberry.com/wml/index.wml", "\n\n";
}
else {
print "Location: http://www.blackberry.com/unsupported.html", "\n\n";
}
}
else {
print "Location: http://www.blackberry.com/index.shtml", "\n\n";
}


They have 2 checks for browser redirecting both have the BlackBerry check one just seems to me that since the device it self has the ability to change the USER_AGENT for different browser emulations they check for both. I'm guessing that I would be better of putting this in a BlackBerry development forum. Thank you for your suggestions, but I just can't seem to get this working correctly with such simple code.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Mobile Browser Redirect

Posted 03 March 2009 - 08:51 PM

Try using the substring:
$x = substr($_SERVER['USER_AGENT'],0,9);

Was This Post Helpful? 1
  • +
  • -

#13 jbrill1012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-March 09

Re: Mobile Browser Redirect

Posted 04 March 2009 - 04:21 AM

YES!!! Okay I got it! Here are the sources I used to learn the best method and to actually be able to fill in the required code:

Russell Beattie's Blog Entry

DB Scripts.net Example Code

along with PHP: strpos - Manual (for reference)

While I was not asking someone to write my code I did ask for help other than an echo. After 4 days of me trying to figure out this problem, I searched enough google and found the two above references which led me to in turn put all of it together and come up with the following code:
<?php
$mobile = $_SERVER['HTTP_USER_AGENT'];
$blackberry = "BlackBerry";
$gomobile = "mobile.htm";
$main = "index1.htm";
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry')) !== false)
{
header("location: $gomobile");
}
else
{
header("location: $main");
}
?>


Hopefully this will save someone the same headache that I've had for the past 4 days trying to figure out a simple way to do a conditional redirection. 14 lines of code(which could be much less) and it's done. :D


This was my first attempt at writting PHP code as I've been through the PHP Tutorial about 4 times now and I still don't have much of a clue. I'm learning slowly but surely how to do something rather than just go grab a script that someone else has written and use it. It comes along the lines of "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." Once the click happens in my head it doesn't take me long to catch on... it's just getting to the point of clicking that's the hard part.


Thanks again for your help maybe I can return the favor some day.
Was This Post Helpful? 0
  • +
  • -

#14 Guest_ghost2012*


Reputation:

Re: Mobile Browser Redirect

Posted 02 July 2010 - 05:39 AM

View Postjbrill1012, on 04 March 2009 - 03:21 AM, said:

YES!!! Okay I got it! Here are the sources I used to learn the best method and to actually be able to fill in the required code:

Russell Beattie's Blog Entry

DB Scripts.net Example Code

along with PHP: strpos - Manual (for reference)

While I was not asking someone to write my code I did ask for help other than an echo. After 4 days of me trying to figure out this problem, I searched enough google and found the two above references which led me to in turn put all of it together and come up with the following code:
<?php
$mobile = $_SERVER['HTTP_USER_AGENT'];
$blackberry = "BlackBerry";
$gomobile = "mobile.htm";
$main = "index1.htm";
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry')) !== false)
{
header("location: $gomobile");
}
else
{
header("location: $main");
}
?>


Hopefully this will save someone the same headache that I've had for the past 4 days trying to figure out a simple way to do a conditional redirection. 14 lines of code(which could be much less) and it's done. :D


This was my first attempt at writting PHP code as I've been through the PHP Tutorial about 4 times now and I still don't have much of a clue. I'm learning slowly but surely how to do something rather than just go grab a script that someone else has written and use it. It comes along the lines of "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." Once the click happens in my head it doesn't take me long to catch on... it's just getting to the point of clicking that's the hard part.


Thanks again for your help maybe I can return the favor some day.



Replace the double quotes around the mobile's redirect location with single quotes. I did and it works perfect for me.
Was This Post Helpful? 0

#15 mlynn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-July 11

Re: Mobile Browser Redirect

Posted 01 July 2011 - 05:55 PM

Redirecting a user based on the type of device they are using is not rocket science. In fact, it can be accomplished quite simply using an apache redirect in a .htaccess file.
RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301]


The issue with this approach is that users coming to your site using an iPad will never have the ability to see the other versions of your site. for many, that may be a satisfactory solution. However, if you want users to have the ability to view a version of your site designed specifically for their type of mobile device by default but have the option to click a link and either view or optionally set a preference for another version of the site, this article is for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1