Mobile Detection and cURL redirect giving 404 - php

So I have a desktop (www.example.com/friends.php) and separate mobile site (m.example.com/friends.php). Not all pages on the desktop site exist on the mobile site.
The Goal: I am detecting a mobile device, then if it proves true, I want to capture the desktop URL after the .com (friends.php), and substitute the mobile domain "m.example.com + $pagename". I then want to detect if the page exists on the mobile domain via cURL (see below). If it exists, I want it to redirect to the corresponding mobile page, if it doesn't exists (returns 404), it should redirect to the base mobile domain.
I currently have the function set to alert if a mobile page is found or not based on the 404.
The issue: when I testa page I know does not exist, it alerts "mobile page found" always, and then will redirect to the mobile site 404 page. What am I missing here? I've stared at this for hours.
The Code:
function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);}
if(isMobile()){
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$pagename = curPageName();
$mobileurl = 'http://m.example.com/' + $pagename;
$curl = curl_init($mobileurl);
curl_setopt($curl, CURLOPT_NOBODY, true);
$result = curl_exec($curl);
if ($result !== false) {
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 404) {
echo "<script type='text/javascript'>alert('mobile url not found');</script>";
header("Location: http://m.example.com/");
} else {
echo "<script type='text/javascript'>alert('mobile url found');</script>";
header("Location:" + $mobileurl);}
} else {
echo "<script type='text/javascript'>alert('mobile url not found');</script>";
header("Location: http://m.example.com/");
}
} else {
return false;
}

Related

Finding status of website with Cloudflare DDoS protection

Trying to check the status of an website using the following code:
<?php
$url = 'https://shop.bbc.com/';
list($status) = get_headers($url);
if (strpos($status, '404') !== FALSE) {
echo '404 error';
} else {
echo $status;
}
?>
However since the website is protected by cloudflare it gives a 403 forbidden error when I try to access it. Is there any known workarounds for this problem?

Identify referrer based on landing URL

I have noticed that when you click on a Google Ads link, it appends this ?gclid=abcdef on url.
Same behavior goes for facebook as well.
I am using the following block of code in order to identify the referrer.
if (strpos($url, 'gclid') !== false) {
$ref = 'Google Adwords';
} else if (strpos($url, 'fbclid') !== false) {
$ref = 'Facebook';
}
Is there any other url identifiers or an other way to get the referrer url? The $_SERVER['HTTP_REFERER'] does not work.

PHP Script not always executing

I seem to be having problems with a PHP script which seems to have some intermittent problems.
Im hoping you can help. The php script is simply used to provide users with different stylesheets dependant on their HTTP agent. (I know this is not responsive mobile design)
This work is for a corporate project therefore unfortunately I cant share any links...
The problem I seem to be having is often this script fails to execute and the user is not directed to appropriate stylesheet. This can be simulated by visiting the page and hitting refresh... When the error occurs and I directly visit the PHP script I am simply provided with the code snippet. When the script is working and I do the same I am directed straight to the appropriate CSS file as expected.
Why could this be? Is this a server side problem or is there something wrong with my implementation?
<?php
// MOBILE
$blackberry = strpos($_SERVER['HTTP_USER_AGENT'],"BB10");
$blackberry2 = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
$chrome = strpos($_SERVER['HTTP_USER_AGENT'],"Chrome");
$safari = strpos($_SERVER['HTTP_USER_AGENT'],"Safari");
// REDIRECTS
// MOBILE
if ($blackberry == true)
{
header('Location: style.css');
}
else if ($blackberry2 == true)
{
header('Location: bb7.css');
}
else if ($iphone == true)
{
header('Location: style.css');
}
else if ($ipad == true)
{
header('Location: ipad.css');
}
else if ($chrome == true)
{
header('Location: style.css');
}
else if ($safari == true)
{
header('Location: style.css');
}
?>
HTML Code...
<!--Dynamic CSS Scripts-->
<link rel="stylesheet" href="css.php">
<!--END-->
Thanks!

Redirect loop in full site to mobile site using session

I have a full site that has been in OS-commerce and mobile site is in core PHP (codeignitor), and full version and a mobile version on sub-domain.
e.g full site: www.example.com and mobile site domain is m.example.com. when user open full site domain in mobile, then website redirect proper mobile domain, But if mobile user want to view full site then user can view fullsite in mobile.
I have used this to complete the redirect http://code.google.com/p/php-mobile-detect/, But it is not redirecting to the full site or to the mobile site using session. I know that I have to use PHP SESSIONS and REQUEST in order to get this to work but I am not sure how to use them in this instance, so could you please suggest how to solve this redirecting issue using session?
Here my code is:
session_start();
include('includes/Mobile_Detect.php');
$detect = new Mobile_Detect;
if(isset($_REQUEST['fullsite']) && $_REQUEST['fullsite'] == 'yes')
{//check if fullsite view request from mobile or website?
$_SESSION['fullsite']="yes";
if($detect->isMobile()) {
$_SESSION['website']="mobile";
}
else{
$_SESSION['website']="computer";
}
$deviceType = header('Location: https://www.example.com/');
}
else
{
if($_SESSION['website'] =="mobile" && $_SESSION['fullsite'] !="yes")
{
if($detect->isTablet())
{
$deviceType = 'tablet';
}
else
{
$deviceType = 'phone';
}
$deviceType = header('Location: https://m.example.com/');
}
elseif($_SESSION['website'] =="computer" && $_SESSION['fullsite'] =="yes")
{
$deviceType = 'computer';
$deviceType = header('Location: https://www.example.com/');
}
else{
$deviceType = 'computer';
}
$scriptVersion = $detect->getScriptVersion();
session_destroy();
}
From what I could get from github page you should be able to make it work like this:
index.php
session_start();
if ($_GET['fullscreen'] == 'yes') {
$_SESSION['fullscreen'] = 1;
} else if ($_GET['fullscreen'] == 'no') {
$_SESSION['fullscreen'] = 0;
}
if (false == isset($_SESSION['fullscreen']) && ($_SESSION['fullscreen'] == 0)) {
// If session['fullscreen'] has not been set (maybe first visit
// or the user does not what in fullscree
// check the device and do redirect
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect();
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
}
...
}
// Other code here
When visiting from mobile, if the user wants the full version, provide an anchor to url with GET parameter fullscreen=yes (http://example.com?fullscreen=yes)
If on full site and detect mobile (not included in code above), you could provide a link to mobile version with fullscreen=no

Mobile site only redirects to main site home page

I am having problems keeping the main site displayed after redirecting from a mobile site.
If a mobile device is detected it redirects to the mobile site. There is a "main site" link on the mobile site, when clicked, which will take you to the main site. For some reason it wont stay on the main site when you click a link on the main site home page, it redirects back to the Mobile site.
I assume the cookie is not storing correctly.
<?php
#include("Mobile_Detect.php");
$detect = new Mobile_Detect();
$allow_mobile = isset($_COOKIE['mobile'])? true:false;
if (isset($_GET['mobile'])) {
if ($_GET['mobile']=='false'){
setcookie("mobile", "");
$allow_mobile = false;
} else {
setcookie("mobile", true, time() + 31536000, "/");
$allow_mobile = true;
}
}
if ($allow_mobile && $detect->isMobile()){
if (!$detect->isTablet()) {
header("Location:http://mobilesite.mobi");
}
}
$not_mobile_cookie = isset($_COOKIE['notmobile'])? true:false;
if (isset($_GET['mobile'])) $not_mobile_cookie = $_GET['mobile'];
if ($not_mobile_cookie==false && $detect->isMobile()){
if (!$detect->isTablet()) {
header("Location:http://mobile.mobi");
}
}
?>
It is probably something simple but I can't see to figure it out.
Thanks!
The key to your question is that mobile devices are still redirected when you click a link on the main site home page.
Your code is testing for a cookie named ['notmobile'] which does not appear to be getting set anywhere. Therefore always evaluates as false, which is why mobile users are being redirected back to the mobile site.
In your code, the purpose of the mobile GET variable is unclear, but taking it to mean that it permits mobile devices to access the main site, I have renamed the variable below to allowMobile.
Assuming Mobile_Detect is functioning correctly, the following code will allow a mobile device to stay on the main site following a allowMobile=true GET request. This can be cancelled with a with allowMobile=false request.
#include("Mobile_Detect.php");
$detect = new Mobile_Detect();
// Do we want to allow a mobile to view the main site content?
// If there is a cookie, yes, if not no.
$allow_mobile = (isset($_COOKIE['mobile']) && $_COOKIE['mobile']) ? true : false;
// If there is a GET allowMobile string saying 'false', delete the cookie and deny access
if (isset($_GET['allowMobile']) && $_GET['allowMobile']=='false') {
// Delete a cookie if one exists
setcookie("mobile", "", time()-1, "/");
$allow_mobile = false;
} elseif (isset($_GET['allowMobile']) {
// if there is any other value for allowMobile, set a cookie allowing mobile access
setcookie("mobile", true, time() + 31536000, "/");
$allow_mobile = true;
}
// If we DO NOT allow mobile, then redirect to the mobile site
if (!$allow_mobile && $detect->isMobile() && !$detect->isTablet()){
header("Location: http://mobilesite.mobi");
exit();
}
// Else, display or redirect to non-mobile page here
This seems to work if anyone else has the same problem. I am not sure if it is even the correct way of doing it but it is working properly for me.
Thank you very much for the help PassKit, much appreciated!
<?php
#include("Mobile_Detect.php");
$detect = new Mobile_Detect();
$mobile_cookie = isset($_COOKIE['mobile'])? $_COOKIE['mobile'] : "";
$force_mobile = ($mobile_cookie == "true") ? true : false;
if (isset($_GET['mobile'])) {
if ($_GET['mobile'] == 'true') { // must we force the mobile site? if ?mobile=true then FORCE THAT MOBILE
setcookie("mobile", "true", time() + 31536000, "/");
$force_mobile = true;
} else { // if ?mobile=false then remove the force
setcookie("mobile", "false");
$force_mobile = false;
}
}
if ($force_mobile){
header("Location:http://mobilesite.mobi");
} else {
if ($detect->isMobile()){
if ($mobile_cookie == "" && !$detect->isTablet()){
header("Location:http://mobilesite.mobi");
}
}
}
?>

Categories