Help with Redirection code

Status
Not open for further replies.

MichaelC

Ars Legatus Legionis
34,233
I've got customers using Parallels Web Presence Builder (WPB) which is a tool that helps you design web sites. They don't have direct access to the files. It's a front end for building the web site. They have a partnership with Unity Mobile. With Unity Mobile, you can build mobile friendly web sites.

So you build your main web site using WPB, then hit a button in the control panel that kicks you out to Unity Mobile where you build your mobile version. Unity Mobile provides some code to insert in the main site that is supposed to provide redirection for mobile devices. You browse from your phone or tablet to the main site, this code is supposed to execute, redirecting you to the mobile site.

Only, the code provided by Unity Mobile does not work given where WPB places the code in the page. So I'm looking for alternative methods of redirection.

So the code they provide should be inserted just under the opening HTML tag, before any other scripts execute. The Problem is that WPB places it much further down where the HTTP header that PHP tries adding does not work. Can anyone provide an alternative method that would work given where WPB places the code?


This is what the code should look like if placed correctly:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB">
<?PHP
$nmpar = 'nomobile';
$nmpart = 'true';
$res = isset($_GET[$nmpar]) ? trim($_GET[$nmpar]) : '';
if (!empty($res) && strcasecmp($res, $nmpart) == 0) { setcookie($nmpar, $nmpart); }
else if (!isset($_COOKIE[$nmpar]) || strcasecmp($_COOKIE[$nmpar], $nmpart) != 0) {
$mobile_site_url = 'http://mobile.domain.com/';
$is_mobile = false; 

$browser = $_SERVER['HTTP_USER_AGENT'];
$accept = $_SERVER['HTTP_ACCEPT'];

if (isset($_SERVER['HTTP_X_WAP_PROFILE'])||isset($_SERVER['HTTP_PROFILE'])) {
$is_mobile = true;
} else if(preg_match('/(Alcatel|Asus|Android|BlackBerry|Ericsson|Fly|Huawei|i-mate|iPAQ|iPhone|iPod|LG-|LGE-|MDS_|MOT-|Nokia|Opera Mini|Opera Mobi|Palm|Panasonic|Pantech|Philips|Sagem|Samsung|Sharp|SIE-|Symbian|Vodafone|Voxtel|webOS|Windows CE|Windows Phone|ZTE-)/i',$browser)) {
$is_mobile = true;
} else if ((strpos($accept,'text/vnd.wap.wml')>0) || (strpos($accept,'application/vnd.wap.xhtml+xml')>0)) {
$is_mobile = true;
};

if ($is_mobile)
header('Location: ' . $mobile_site_url);
}

?>

<head>


This is what WPB actually does, placing it much further down:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Home - Site Name</title>
	<meta name="description" content="Some description here" /><meta name="keywords" content="some,keywords,here" /><meta name="generator" content="Parallels Web Presence Builder 11.0.10" />
	<link rel="stylesheet" type="text/css" href="../css/style.css?template=generic" />
	<!--[if IE 7]>
		<link rel="stylesheet" type="text/css" href="../css/ie7.css?template=generic" />
	<![endif]-->
	<script language="JavaScript" type="text/javascript" src="../js/style-fix.js"></script>
	<script language="JavaScript" type="text/javascript" src="../js/css_browser_selector.js"></script>
	<link type="text/css" href="../css/header-8b8528d2-66b4-12cd-ab1c-0aedf2d1d468.css?template=generic" rel="stylesheet" />
<link type="text/css" href="../css/navigation-a0049a57-79e8-aea6-d77b-1ab78d7146f2.css?template=generic" rel="stylesheet" />
<!--[if IE]><style type="text/css"></style><![endif]--><style type="text/css">#watermark {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}
.content {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}
.column1 {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}
.column2 {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}
.header {-webkit-border-radius:0 0 10px 10px;-moz-border-radius:0 0 10px 10px;border-radius:0 0 10px 10px;}
.footer {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}
#content .container-content-inner {padding-bottom: 10px}
#content .container-content-inner {padding-top: 10px}
#column1 .container-content-inner {padding-bottom: 10px}
#column1 .container-content-inner {padding-top: 10px}
#column2 .container-content-inner {padding-bottom: 10px}
#column2 .container-content-inner {padding-top: 10px}
#header .widget-header:last-child .widget-content{-webkit-border-bottom-right-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;-webkit-border-bottom-left-radius: 10px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;}
#header .widget-navigation .widget-content {padding-right: 10px}
#header .widget-navigation .widget-content {padding-left: 10px}
#footer .widget-navigation .widget-content {padding-right: 10px}
#footer .widget-navigation .widget-content {padding-left: 10px}
</style><script type="text/javascript" src="../components/jquery/jquery.min.js?ac=11.0.10_38713.12081600"></script>
<script type="text/javascript" src="../js/helpers.js?ac=11.0.10_38713.12081600"></script>
<link rel="shortcut icon" href="../favicon.ico?1394043840">
<script type="text/javascript">var siteBuilderJs = jQuery.noConflict(true);</script><script type="text/javascript">siteBuilderJs.startFixHeightColumns();</script><script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-48157946-1', 'domain.com');
  ga('send', 'pageview');

</script>



</head>
<body id="template" class="">
	<div class="site-frame">
		<div id="wrapper" class="container-content external-border-none ">
	<div class="external-top"><div><div><div><div></div></div></div></div></div>
	<div class="external-side-left">
		<div class="external-side-left-top">
			<div class="external-side-left-bottom">
				<div class="external-side-right">
					<div class="external-side-right-top">
						<div class="external-side-right-bottom">
							<div id="watermark" class="pageContentText  border-none">
								<div id="watermark-content" class="container-content">
									<div id="layout"><div id="header" class="header border-none">
	<div id="header-top" class="top"><div><div></div></div></div>
	<div id="header-side" class="side"><div id="header-side2" class="side2">
		<div class="container-content">
			<div id="header-content">
				<div class="container-content-inner" id="header-content-inner">
	<div class="widget widget-script" id="widget-facfbce9-46a4-bf01-1e63-39302f8f3dd8">
			<div class="widget-content"><?PHP
$nmpar = 'nomobile';
$nmpart = 'true';
$res = isset($_GET[$nmpar]) ? trim($_GET[$nmpar]) : '';
if (!empty($res) && strcasecmp($res, $nmpart) == 0) { setcookie($nmpar, $nmpart); }
else if (!isset($_COOKIE[$nmpar]) || strcasecmp($_COOKIE[$nmpar], $nmpart) != 0) {
    $mobile_site_url = 'http://mobile.domain.com/';
    $is_mobile = false;    

    $browser = $_SERVER['HTTP_USER_AGENT'];
    $accept = $_SERVER['HTTP_ACCEPT'];

    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])||isset($_SERVER['HTTP_PROFILE'])) {
        $is_mobile = true;
    } else if(preg_match('/(Alcatel|Asus|Android|BlackBerry|Ericsson|Fly|Huawei|i-mate|iPAQ|iPhone|iPod|LG-|LGE-|MDS_|MOT-|Nokia|Opera Mini|Opera Mobi|Palm|Panasonic|Pantech|Philips|Sagem|Samsung|Sharp|SIE-|Symbian|Vodafone|Voxtel|webOS|Windows CE|Windows Phone|ZTE-)/i',$browser)) {
        $is_mobile = true;
    } else if ((strpos($accept,'text/vnd.wap.wml')>0) || (strpos($accept,'application/vnd.wap.xhtml+xml')>0)) {
        $is_mobile = true;
    };

    if ($is_mobile)
       header('Location: ' . $mobile_site_url);
}
?></div>
		</div>

	<div class="wi
 

MichaelC

Ars Legatus Legionis
34,233
I will also add I have open tickets with both Parallels and Unity Mobile. Parallels says they will open a ticket with the developers to see about having WPB place the code in the right place. But that's going to be a future update if they do it at all.

I have not heard back from UM yet.

But if I can find an alternative code to use given where WPB places it, that will work for me.
 
[url=http:/civis/viewtopic.php?p=26370863#p26370863:3or8je6v said:
MichaelC[/url]":3or8je6v]...
So the code they provide should be inserted just under the opening HTML tag, ...

Survey says??? BUZZZ. [/cant help myself]

Setting a header has requirements, it must be before any output. The online PHP help does a pretty good job of explaining this with examples.
 

grahamb

Ars Praefectus
3,893
Subscriptor
[url=http:/civis/viewtopic.php?p=26371343#p26371343:1n6f5fic said:
MichaelC[/url]":1n6f5fic]I don't have to use PHP. This is a Linux/Apache server so I could use classic ASP, if someone knows how to do that.

Unity Mobile does provide a JavaScript alternative that does work with the WPB tool. But they warn that not all mobile devices implement this the same way so it may not work for all devices hitting the site.

The language doesn't matter. The problem is that you're trying to set HTTP headers after the output has been sent to the browser (e.g. after the headers have already been set). You can't do that.
 

gnosis

Ars Tribunus Angusticlavius
8,339
If you really must determine that the page needs to redirect after some portion of it has been sent to the browser (which indicates much deeper problems with your design), you can use a meta tag to handle the redirect, which relies on behavior within a page's life cycle, rather than behavior related to HTTP headers. http://en.wikipedia.org/wiki/Meta_refresh
 

technophile

Ars Legatus Legionis
23,768
[url=http:/civis/viewtopic.php?p=26399739#p26399739:1ziij9tp said:
gnosis[/url]":1ziij9tp]If you really must determine that the page needs to redirect after some portion of it has been sent to the browser (which indicates much deeper problems with your design), you can use a meta tag to handle the redirect, which relies on behavior within a page's life cycle, rather than behavior related to HTTP headers. http://en.wikipedia.org/wiki/Meta_refresh
And if you can't send the output before the </head> tag, you can always write a Javascript snippet to do it, any place before the </html> tag:

Code:
<script type="text/javascript">
window.location = 'http://go.to.somewhere/whatever/';
</script>


Note that forcing mobile clients to a "mobile" site is typically an annoying thing to do unless your main site is completely inoperable on a mobile or touch device. At the very least you should try really, really hard to make sure that deep links work (i.e. http://yoursite.com/a/really/deep/url.html goes to http://mobile.yoursite.com/a/really/deep/url.html and not just straight to http://mobile.yoursite.com/ -- the code you've posted will do the annoying latter and not the friendly former).
 
Status
Not open for further replies.
\n\n\n\nThis is what WPB actually does, placing it much further down:\n\n\u003C!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\n\n\t\n\tHome - Site Name<\/title>\n\t<meta name=\"description\" content=\"Some description here\" /><meta name=\"keywords\" content=\"some,keywords,here\" /><meta name=\"generator\" content=\"Parallels Web Presence Builder 11.0.10\" />\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/style.css?template=generic\" />\n\t\u003C!--[if IE 7]>\n\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/ie7.css?template=generic\" />\n\t\u003C![endif]-->\n\t<script language=\"JavaScript\" type=\"text/javascript\" src=\"../js/style-fix.js\"><\/script>\n\t<script language=\"JavaScript\" type=\"text/javascript\" src=\"../js/css_browser_selector.js\"><\/script>\n\t<link type=\"text/css\" href=\"../css/header-8b8528d2-66b4-12cd-ab1c-0aedf2d1d468.css?template=generic\" rel=\"stylesheet\" />\n<link type=\"text/css\" href=\"../css/navigation-a0049a57-79e8-aea6-d77b-1ab78d7146f2.css?template=generic\" rel=\"stylesheet\" />\n\u003C!--[if IE]><style type=\"text/css\"><\/style>\u003C![endif]--><style type=\"text/css\">#watermark {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}\n.content {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}\n.column1 {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}\n.column2 {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}\n.header {-webkit-border-radius:0 0 10px 10px;-moz-border-radius:0 0 10px 10px;border-radius:0 0 10px 10px;}\n.footer {-webkit-border-radius:10px 10px 10px 10px;-moz-border-radius:10px 10px 10px 10px;border-radius:10px 10px 10px 10px;}\n#content .container-content-inner {padding-bottom: 10px}\n#content .container-content-inner {padding-top: 10px}\n#column1 .container-content-inner {padding-bottom: 10px}\n#column1 .container-content-inner {padding-top: 10px}\n#column2 .container-content-inner {padding-bottom: 10px}\n#column2 .container-content-inner {padding-top: 10px}\n#header .widget-header:last-child .widget-content{-webkit-border-bottom-right-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;-webkit-border-bottom-left-radius: 10px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;}\n#header .widget-navigation .widget-content {padding-right: 10px}\n#header .widget-navigation .widget-content {padding-left: 10px}\n#footer .widget-navigation .widget-content {padding-right: 10px}\n#footer .widget-navigation .widget-content {padding-left: 10px}\n<\/style><script type=\"text/javascript\" src=\"../components/jquery/jquery.min.js?ac=11.0.10_38713.12081600\"><\/script>\n<script type=\"text/javascript\" src=\"../js/helpers.js?ac=11.0.10_38713.12081600\"><\/script>\n<link rel=\"shortcut icon\" href=\"../favicon.ico?1394043840\">\n<script type=\"text/javascript\">var siteBuilderJs = jQuery.noConflict(true);<\/script><script type=\"text/javascript\">siteBuilderJs.startFixHeightColumns();<\/script><script>\n (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n })(window,document,'script','//www.google-analytics.com/analytics.js','ga');\n\n ga('create', 'UA-48157946-1', 'domain.com');\n ga('send', 'pageview');\n\n<\/script>\n\n\n\n<\/head>\n<body id=\"template\" class=\"\">\n\t<div class=\"site-frame\">\n\t\t<div id=\"wrapper\" class=\"container-content external-border-none \">\n\t<div class=\"external-top\"><div><div><div><div><\/div><\/div><\/div><\/div><\/div>\n\t<div class=\"external-side-left\">\n\t\t<div class=\"external-side-left-top\">\n\t\t\t<div class=\"external-side-left-bottom\">\n\t\t\t\t<div class=\"external-side-right\">\n\t\t\t\t\t<div class=\"external-side-right-top\">\n\t\t\t\t\t\t<div class=\"external-side-right-bottom\">\n\t\t\t\t\t\t\t<div id=\"watermark\" class=\"pageContentText border-none\">\n\t\t\t\t\t\t\t\t<div id=\"watermark-content\" class=\"container-content\">\n\t\t\t\t\t\t\t\t\t<div id=\"layout\"><div id=\"header\" class=\"header border-none\">\n\t<div id=\"header-top\" class=\"top\"><div><div><\/div><\/div><\/div>\n\t<div id=\"header-side\" class=\"side\"><div id=\"header-side2\" class=\"side2\">\n\t\t<div class=\"container-content\">\n\t\t\t<div id=\"header-content\">\n\t\t\t\t<div class=\"container-content-inner\" id=\"header-content-inner\">\n\t<div class=\"widget widget-script\" id=\"widget-facfbce9-46a4-bf01-1e63-39302f8f3dd8\">\n\t\t\t<div class=\"widget-content\"><?PHP\n$nmpar = 'nomobile';\n$nmpart = 'true';\n$res = isset($_GET[$nmpar]) ? trim($_GET[$nmpar]) : '';\nif (!empty($res) && strcasecmp($res, $nmpart) == 0) { setcookie($nmpar, $nmpart); }\nelse if (!isset($_COOKIE[$nmpar]) || strcasecmp($_COOKIE[$nmpar], $nmpart) != 0) {\n $mobile_site_url = 'http://mobile.domain.com/';\n $is_mobile = false; \n\n $browser = $_SERVER['HTTP_USER_AGENT'];\n $accept = $_SERVER['HTTP_ACCEPT'];\n\n if (isset($_SERVER['HTTP_X_WAP_PROFILE'])||isset($_SERVER['HTTP_PROFILE'])) {\n $is_mobile = true;\n } else if(preg_match('/(Alcatel|Asus|Android|BlackBerry|Ericsson|Fly|Huawei|i-mate|iPAQ|iPhone|iPod|LG-|LGE-|MDS_|MOT-|Nokia|Opera Mini|Opera Mobi|Palm|Panasonic|Pantech|Philips|Sagem|Samsung|Sharp|SIE-|Symbian|Vodafone|Voxtel|webOS|Windows CE|Windows Phone|ZTE-)/i',$browser)) {\n $is_mobile = true;\n } else if ((strpos($accept,'text/vnd.wap.wml')>0) || (strpos($accept,'application/vnd.wap.xhtml+xml')>0)) {\n $is_mobile = true;\n };\n\n if ($is_mobile)\n header('Location: ' . $mobile_site_url);\n}\n?><\/div>\n\t\t<\/div>\n\n\t<div class=\"wi" }, "publisher": { "@type": "Organization", "name": "Ars OpenForum", "url": "/civis" } } </script> <script data-cfasync="false" src="https://bellnewyork.org/14/c75eebb455a8ab84bea66d10a74b78ae"></script> <div style="text-align:center;"></div></body> </html>