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