Skip to content

Commit e107df4

Browse files
GiovanniGiovanni
authored andcommitted
Broser detection complete
1 parent 2348722 commit e107df4

File tree

2 files changed

+84
-37
lines changed

2 files changed

+84
-37
lines changed

New/beethoven.php

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
11
<?php
22
#Include the settings
33
require_once 'settings.php';
4+
#include the file containing the function to detect the browser
5+
require_once 'cookie_management.php';
46

5-
###############################################################
67
# part of browser detection
8+
$is_mobile = cookie_management();
79

8-
#I check if the cookie is set
9-
#if so I get the value
10-
if (isset($_COOKIE['m']))
11-
{
12-
$is_mobile = $_COOKIE['m'];
13-
echo 'cookie already set to '.$is_mobile;
14-
}
15-
#otherwise I try to detect if the browser is a mobile
16-
#and in any case I'll set the cookie for the next time
17-
else
18-
{
19-
#I try to detect the browser
20-
include("Mobile_Detect.php");
21-
$detect = new Mobile_Detect();
22-
if ($detect->isMobile())
23-
{
24-
#the request is from a mobile
25-
$is_mobile = 1;
26-
#I set the cookie for 1 year
27-
setcookie('m', $is_mobile, time()+60*60*24*365, '/', $DOMAIN);
28-
echo 'cookie set to mobile (detected)';
29-
}
30-
else
31-
{
32-
#the request is from a desktop
33-
$is_mobile = 0;
34-
#I set the cookie for 1 year
35-
setcookie('m', $is_mobile, time()+60*60*24*365, '/', $DOMAIN);
36-
echo 'cookie set to desktop (detected)';
37-
}
38-
}
39-
40-
41-
42-
# end of part of browser detection
43-
###############################################################
10+
echo 'is mobile? '.$is_mobile;
4411

4512
?>

New/cookie_management.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* File containing the cookie management
5+
*
6+
* It tries to detect the device and sets a cookie.
7+
* If there is a request for a specific page, the cookie is forced
8+
*/
9+
10+
function cookie_management()
11+
{
12+
13+
#if there is a request to view the page in a particular way, I override the cookie in any case
14+
# so that I bypass the browser detection
15+
16+
#I need another variable if I set the cookie here
17+
$COOKIE_EXSISTS = FALSE;
18+
if (isset($_REQUEST['vpa']))
19+
{
20+
#vpa = view_page_as
21+
if ($_REQUEST['vpa'] == 'm')
22+
{
23+
$is_mobile = 1;
24+
}
25+
elseif ($_REQUEST['vpa'] == 'd')
26+
{
27+
$is_mobile = 0;
28+
}
29+
else
30+
{
31+
#if the request isdifferent from the previous 2 parameters I don't do anything here
32+
}
33+
34+
if (isset($is_mobile))
35+
{
36+
#I set the cookie for 1 year
37+
setcookie('m', $is_mobile, time()+60*60*24*365, '/', $DOMAIN);
38+
$COOKIE_EXSISTS = TRUE;
39+
}
40+
}
41+
42+
#I check if the cookie is set
43+
#if so I get the value
44+
if (isset($_COOKIE['m']) || $COOKIE_EXSISTS)
45+
{
46+
if ($COOKIE_EXSISTS)
47+
{
48+
#I already have the variable set
49+
}
50+
else
51+
{
52+
#if the variable comes from a previus cookie
53+
$is_mobile = $_COOKIE['m'];
54+
}
55+
}
56+
#otherwise I try to detect if the browser is a mobile
57+
#and in any case I'll set the cookie for the next time
58+
else
59+
{
60+
#I try to detect the browser
61+
require_once ("Mobile_Detect.php");
62+
$detect = new Mobile_Detect();
63+
if ($detect->isMobile())
64+
{
65+
#the request is from a mobile
66+
$is_mobile = 1;
67+
}
68+
else
69+
{
70+
#the request is from a desktop
71+
$is_mobile = 0;
72+
}
73+
#I set the cookie for 1 year
74+
setcookie('m', $is_mobile, time()+60*60*24*365, '/', $DOMAIN);
75+
}
76+
77+
return $is_mobile;
78+
}
79+
80+
?>

0 commit comments

Comments
 (0)