Stefan83 Posted October 4, 2013 Share Posted October 4, 2013 I'm using the mobiledetect.net script to redirect mobile users to a mobile page. I'd like to dynamically add the referrers URI to the URL string ie /?source=previous-page. For example, when visiting http://mainsite.com/landing-page you are redirected to http://www.mobiledomain.com/?source=previous-pageIs this possible? <?php @include("Mobile_Detect.php"); $detect = new Mobile_Detect(); if ($detect->isMobile() && isset($_COOKIE['mobile'])) { $detect = "false"; } elseif ($detect->isMobile()) { header("Location:http://www.mobiledomain.com/"); } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 4, 2013 Share Posted October 4, 2013 (edited) Not sure but maybe $source = ''; if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/') $source = '?source=' . $_SERVER['REQUEST_URI']; header("Location:http://www.mobiledomain.com/$source"); Now $_GET['source'] will contain the request url before being redirected to your mobile site. Edited October 4, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 4, 2013 Share Posted October 4, 2013 You'll want to urlencode the url in the query string. $source = '?source=' . urlencode($_SERVER['REQUEST_URI']); just in case there are any "special" characters in it. Do you want the url of the page that is doing the redirect (which is what Ch0cu3r's code gives you)? Or do you want the url of the the page they came from to get to the page that is doing the redirect? In the second case, use $_SERVER['HTTP_REFERER'] instead of 'REQUEST_URI'. Quote Link to comment Share on other sites More sharing options...
Stefan83 Posted October 4, 2013 Author Share Posted October 4, 2013 That's great, thanks guys. Almost there, but it returns ?from= twice and the current page in the string as well. So the URL for the landing page is http://mobiledomain.com/mobile/?from=%2Fmobile%2F%3Ffrom%3D%252Fpassengers%252Ftaxybikes%252F My code is <?php include('Mobile_Detect.php'); $url = get_bloginfo('url'); $detect = new Mobile_Detect(); if ($detect->isMobile() && isset($_COOKIE['mobile'])) { $detect = "false"; } elseif ($detect->isMobile()) { $source = ''; if(isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/'); $source = '?from=' . urlencode($_SERVER['REQUEST_URI']); header("Location:http://mobiledomain.com/mobile/$source"); } ?> ANy ideas why that is? Thanks Quote Link to comment Share on other sites More sharing options...
Stefan83 Posted October 8, 2013 Author Share Posted October 8, 2013 Ok, I've got it. It was becuase the script was loading in the header of the wordpress file so there was repitition. Thanks for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.