Endless loop after upgrading to new php 7 server - php

I was using this code to redirect mobile users to my mobile site
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i";
if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap'))
{
$Loaction = 'mobile/';
if (!empty($Loaction))
{
ecs_header("Location: $Loaction\n");
exit;
}
}
domain.com/mobile
Recently i have moved my server to a cloud server with PHP 7 now the issue i have started facing is when the site is accessed by mobile there is this endless loop like this
domain.com/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/mobile/
Can someone please help me or point me in the right direction
Thanks

You must also check that the $_SERVER['REQUEST_URI'] doesn't start with /mobile. For example:
if (($ua == '' || preg_match($uachar, $ua)) &&
strpos($requestUri, '/wap') === false &&
strpos($requestUri, '/mobile') === false
) {
$location = 'mobile/';
header("Location: $location");
exit;
}

Related

PHP how to do elseif final else statement to cover all other posibilities

I have a georedirect code for cloudflare which goes as follows
<?php
$URI = $_SERVER['REQUEST_URI'];
$activepage = $_SERVER['SERVER_NAME'];
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
if ($activepage=="www.example.co.uk" & $country_code=="CA") {
$link = 'http://www.example.us' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.co.uk" & $country_code=="US") {
$link = 'http://www.example.us' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.us" & $country_code=="GB") {
$link = 'http://www.example.co.uk' . $URI;
header("location:$link");
exit;
}
elseif ($activepage=="www.example.us" & $country_code=="UK") {
$link = 'http://www.example.co.uk' . $URI;
header("location:$link");
exit;
}
?>
effectively redirects UK/GB users from the US site back to the UK site, and US & Canadian users on the UK site over to the USD$ site (.us)
I want to add a final else statement so that all traffic which is not US or CA on the US site is redirected to the UK site (so for the rest of the world).
How am I best going about this?
Also this seems to be taking a long time to run (on a good server) how can I streamline this so it only runs as far as it needs to? E.g. the other elseif are not run in the background after the correct one is met? I thought the exit(); would do the trick but apparently not.
Thanks
So you need to add a new elseif that identifies when "site is US, and traffic is not US, and traffic is not CA" and is redirected to the UK site.
Here is an idea of how to do that - I've tidied up the code a bit, married the conditions which had the same outcome, and tidied the logic (without changing it). It's not tested, but use it how you want:
$URI = $_SERVER['REQUEST_URI'];
$activePage = $_SERVER['SERVER_NAME'];
$countryCode = $_SERVER["HTTP_CF_IPCOUNTRY"];
$redirectTld = '';
if (
$activePage == 'www.example.co.uk'
&& ($countryCode == 'CA' || $countryCode == 'US')
) {
$redirectTld = 'us';
} elseif (
$activePage == 'www.example.us'
&& ($countryCode == 'GB' || $countryCode == 'UK')
) {
$redirectTld = 'co.uk';
} elseif (
$activePage == 'www.example.us'
&& $countryCode != 'US'
&& $countryCode != 'CA'
) {
$redirectTld = 'co.uk';
}
if ($redirectTld) {
header('location: http://www.example.' . $redirectTld . $URI);
exit;
}
Note that this won't be a catch all as the conditions need to be met still, if you want a true catch all you'll need to just have an } else { with no conditions (but that didn't seem to be what your question asked for :) )
You could tidy it up even further by getting the page's TLD instead of full URL for $activePage and just checking the url (but I don't know how all your data/info works).
Also this seems to be taking a long time to run (on a good server) how
can I streamline this so it only runs as far as it needs to? E.g. the
other elseif are not run in the background after the correct one is
met? I thought the exit();
This tiny block of conditions shouldn't be a bottle neck, it could be the redirect time (redirecting and connecting to another URL or even server?), or something else.
PHP will stop evaluating a condition as soon as it cannot be met, and once one is met wont evaluate the others because they are "else".
https://stackoverflow.com/a/2535578/2632129

Redirecting some of my website trafic to another URL, referrer based

My website is http://www.example.com
I would like to redirect 15% of the Google/Bing incoming trafic to my website, to a subdomain such as http://beta.example.com
I can use php, htaccess, ...
Thank you for your help.
Something like this?
$referer = $_SERVER['HTTP_REFERER'];
if(strpos($referer, 'bing') !== false || strpos($referer, 'google') !== false) {
if(rand(0, 100) < 15) {
header('Location: http://beta.example.com/');
exit();
}
}

How to detect if user is using an app or browser to access my site?

Hi guys i'm using a video cms script which has a desktop version and a mobile fallback version. The user agent is detected by a file named detect.php and directs the user either to the desktop version or the mobile version of the site. I created a webview app (both android and ios) which I have advertisements in so I want to keep mobile users locked into those apps and remove access if they don't have the app by redirecting them to install my app. I've seen a few suggestions but they don't take into account hybrid sites and I fear that I may end up redirecting people who use my apps into an infinite loop to install an app they already have. Below I have provided my detect.php code...I have also taken a look at google analytics and see safari, safari-in-app, android, etc. So i'm not exactly sure how to take the approach for this because of the many factors involved.
$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragent = trim(strtolower($useragent));
$new_URL = str_replace("www.", '', _URL);
$new_URL_MOBI = str_replace("www.", '', _URL_MOBI);
$current_URL = str_replace("www.", '', "http://".$_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
$current_URL_device = strpos($current_URL, $new_URL_MOBI);
$device_url = ($current_URL_device === false) ? 'desktop' : 'mobile';
if ( ! defined('COOKIE_DEVICE'))
{
define('COOKIE_DEVICE', 'melody_device');
}
if(isset($_COOKIE[COOKIE_DEVICE]))
{
if ($_COOKIE[COOKIE_DEVICE] != $device_url)
{
if ($device_url == 'mobile')
{
$takemehome = str_replace($new_URL_MOBI, $new_URL, $current_URL);
header("Location: ".$takemehome."");
exit;
}
elseif ($device_url == 'desktop')
{
$takemehome = str_replace($new_URL, $new_URL_MOBI, $current_URL);
header("Location: ".$takemehome."");
exit;
}
}
}
else
{
if (strpos($useragent, "iphone") !== false || strpos($useragent, "symbianos") !== false || strpos($useragent, "ipad") !== false || strpos($useragent, "nook") !== false || strpos($useragent, "kindle") !== false || strpos($useragent, "IEMobile") !== false || strpos($useragent, "windows phone") !== false || strpos($useragent, "ipod") !== false || strpos($useragent, "android") !== false || strpos($useragent, "blackberry") !== false || strpos($useragent, "samsung") !== false || strpos($useragent, "nokia") !== false || strpos($useragent, "windows ce") !== false || strpos($useragent, "sonyericsson") !== false || strpos($useragent, "webos") !== false || strpos($useragent, "wap") !== false || strpos($useragent, "motor") !== false || strpos($useragent, "symbian") !== false || strpos($useragent, "android") !== false)
{
$device = 'mobile';
setcookie(COOKIE_DEVICE, $device, time() + 84000, COOKIE_PATH);
if (strpos($current_URL, $new_URL_MOBI) === false)
{
//User is NOT on mobile site, redirect
$takemehome = str_replace($new_URL, $new_URL_MOBI, $current_URL);
header("Location: ".$takemehome."");
exit;
}
//Continue as usual
}
else
{
$device = 'desktop';
setcookie(COOKIE_DEVICE, $device, time() + 84000, COOKIE_PATH);
if (strpos($current_URL, $new_URL_MOBI) !== false)
{
//User IS on mobile site, redirect
$takemehome = str_replace($new_URL_MOBI, $new_URL, $current_URL);
header("Location: ".$takemehome."");
exit;
}
//Continue as usual
}
}
?>
you can't. When an user access to your website from the app, he use in any case the standard browser of the device (in iOS it is Safari). So, you can to know this, just if the app is a your app which you can develop ad hoc by sending an extra POST or GET data to uncover if access is from your app.
In general, as i say, you can't.

"VIEW FULL SITE" mobile site option

So I'm working on the mobile version of a site I'm doing, and
so far, I'm pulling the mobile sites content from its main counterpart, the main site.
As I study some mobile sites out there, I notice a lot of em have a "view full site" link.
Now I plan on redirecting the mobile visitors via .js in the header tag on main site via a check for screen width etc...(not sure if its the best way but so far the easiest on my brain))(but suggestions also welcome)
but something like this
if (screen.width<=XyZ||screen.height<=XyZ) //example iphone size lets say 320x480
window.location.replace("mobile site link here.")
Again I dont know if this is the best way but, on dummy tests, it works on iPhone, some friends Droids, and one Blackberry. But it works.
Anyways, so my question is, if i do this check on every page...how can I possible have a "view full site" option?
Use PHP to detect mobile users through $_SERVER['HTTP_USER_AGENT'].
JavaScript detection may not be reliable, because many mobile browsers do not support JS.
A "View Full Site" will set a cookie to reject mobile site, which is detectable.
Use cookies to keep track of your user's preferences.
In skeleton
<?php
if (isset($_COOKIE['nomobile'])) {
$style = "normal";
} else {
if (preg_match('/iPhone|(...etc...)/', $_SERVER['HTTP_USER_AGENT'])) {
$style = "mobile";
} else {
$style = "normal";
}
}
For the "View Full Site" page:
Full Site
fullsite.php
<?php
setcookie('nomobile', 'true');
header('Location: index.php');
?>
First, go to the following URL and download the mobile_detect.php file:
http://code.google.com/p/php-mobile-detect/
Next, follow the instructions on the page, and upload the mobile_detect.php to your root directory,
Insert the following code on your index or home page:
<?php
#include("Mobile_Detect.php");
$detect = new Mobile_Detect();
if ($detect->isMobile() && isset($_COOKIE['mobile']))
{
$detect = "false";
}
elseif ($detect->isMobile())
{
header("Location:http://www.yourmobiledirectory.com");
}
?>
You will notice that the above code is checking for a cookie called "mobile", this cookie is set when the mobile device is redirected to the mobile page. To set the cookie insert the following code on your mobile landing page:
<?php
setcookie("mobile","m", time()+3600, "/");
?>
View the full article at: http://www.squidoo.com/php-mobile-redirect
It's not a best way, because very often JS aren't supported by mobile browsers.
You can use this function:
function its_mobile_browser($user_agent = '')
{
if (empty($user_agent))
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (empty($user_agent)) return false;
}
if (stripos($user_agent, 'Explorer')!==false ||
stripos($user_agent, 'Windows')!==false ||
stripos($user_agent, 'Win NT')!==false ||
stripos($user_agent, 'FireFox')!==false ||
stripos($user_agent, 'linux')!==false ||
stripos($user_agent, 'unix')!==false ||
stripos($user_agent, 'Macintosh')!==false
)
{
if (!(stripos($user_agent, 'Opera Mini')!==false
|| stripos($user_agent, 'WAP')!==false
|| stripos($user_agent, 'Mobile')!==false
|| stripos($user_agent, 'Symbian')!==false
|| stripos($user_agent, 'NetFront')!==false
|| stripos($user_agent, ' PPC')!==false
|| stripos($user_agent, 'iPhone')!==false
|| stripos($user_agent, 'Android')!==false
|| stripos($user_agent, 'Nokia')!==false
|| stripos($user_agent, 'Samsung')!==false
|| stripos($user_agent, 'SonyEricsson')!==false
|| stripos($user_agent, 'LG')!==false
|| stripos($user_agent, 'Obigo')!==false
|| stripos($user_agent, 'SEC-SGHX')!==false
|| stripos($user_agent, 'Fly')!==false
|| stripos($user_agent, 'MOT-')!==false
|| stripos($user_agent, 'Motorola')!==false
)
) return false;
}
return true;
}
Or something better, lol :)
You can add a query string parameter to your website address such as ?fullsite=true and include the following in your if condition >
var fullsite = getQueryString()["fullsite"];
if (fullsite != "true" && (screen.height <= xyz || screen.width <= abc)) //now redirect
You'll need the following function access query string. I took it from here > JavaScript query string
function getQueryString() {
var result = {}, queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
while (m = re.exec(queryString)) {
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return result;
}
And in the link you can have >
Show me Full Site
===========
Saying that please take a look at CSS Media Queries. It may require changing a bit of your design architecture but it's pretty useful.
Server-side detection is definitely the way to do this, as you have no guarantee of JS being available or even turned on. A great PHP script for mobile detection is found here http://detectmobilebrowsers.mobi/ and it gets a lot of use around the web.

Redirecting to a new page when a user is using Internet Explorer

I am using the code below in order to redirect the users who use Internet Explorer to a new page, but obviously there is something wrong with the code, since the site doesn't load anymore when I am using Internet Explorer.
Here is the code:
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
$url = htmlspecialchars($_GET['url']);
header( 'Location: http://'.$url.'' ) ;
}
?>
Since I don't know what I am doing wrong it would be greatly appreciated if someone could post the right way to do it with the right coding.
Thanks in advance.
You can use get_browser() to get the user browser and then use the if condition.
You can try this:
$browser = get_browser(null, true);
if($browser['browser'] == "Internet Explorer"){
$url = htmlspecialchars($_GET['url']);
header( 'Location: http://'.$url.'' );
} else {
// do something...
}
strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
!== typo ? It should be something like
strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == TRUE)
For modern IE you can use:
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false) {
header('Location: ie-page.php');
exit;
} else {
print "Hello World";
}

Categories