session_example.php
4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* MIT License
* ===========
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* -----------------------------------------------------------------------
*
*
* Run this in your browser to see the example!
*
*
*
* IMPORTANT: Clear your sessions/cookies before running UA tests.
*
* This is a procedural approach example of how to switch your website layout
* based on a variable $layoutType.
*
* The example also includes the switch links that you can put in the footer
* of your page. Is a good practice to let the user switch between layouts,
* even if he is visiting the page from a phone or tablet.
* ------------------------------------------------------------------------
*
* @author Serban Ghita <serbanghita@gmail.com>
* @license MIT License https://github.com/serbanghita/Mobile-Detect/blob/master/LICENSE.txt
*
*/
// This is mandatory if you're using sessions.
session_start();
// It's mandatory to include the library.
require_once '../Mobile_Detect.php';
/**
* Begin helper functions.
*/
// Your default site layouts.
// Update this array if you have fewer layout types.
function layoutTypes()
{
return array('classic', 'mobile', 'tablet');
}
function initLayoutType()
{
// Safety check.
if (!class_exists('Mobile_Detect')) { return 'classic'; }
$detect = new Mobile_Detect;
$isMobile = $detect->isMobile();
$isTablet = $detect->isTablet();
$layoutTypes = layoutTypes();
// Set the layout type.
if ( isset($_GET['layoutType']) ) {
$layoutType = $_GET['layoutType'];
} else {
if (empty($_SESSION['layoutType'])) {
$layoutType = ($isMobile ? ($isTablet ? 'tablet' : 'mobile') : 'classic');
} else {
$layoutType = $_SESSION['layoutType'];
}
}
// Fallback. If everything fails choose classic layout.
if ( !in_array($layoutType, $layoutTypes) ) { $layoutType = 'classic'; }
// Store the layout type for future use.
$_SESSION['layoutType'] = $layoutType;
return $layoutType;
}
/**
* End helper functions.
*/
// Let's roll. Call this function!
$layoutType = initLayoutType();
/**
*
* Example of layout switch links.
* Eg. ['Classic' | Mobile | 'Tablet']
*
*/
?>
<?php if(!isset($_GET['page'])): ?>
<!-- example page #1 -->
<h1>Demo page number one.</h1>
<p>You can go to page <a href="<?php echo $_SERVER['PHP_SELF']; ?>?page=two">two</a>.</p>
<p>Showing you the <b><?php echo $layoutType; ?></b> version.</p>
<p><b>Note:</b> When running this test using the same browser with multiple User-Agents, clear your cookies/session before each test.</p>
<?php endif; ?>
<?php if(isset($_GET['page']) && $_GET['page']=='two'): ?>
<!-- example page #2 -->
<h1>Demo page number two.</h1>
<p>You can go back to page <a href="<?php echo $_SERVER['PHP_SELF']; ?>">one</a>.</p>
<p>Showing you the <b><?php echo $layoutType; ?></b> version.</p>
<?php endif; ?>
<!-- Footer links example. Change this as you like. -->
<?php foreach(layoutTypes() as $_layoutType): ?>
<?php if($_layoutType == $layoutType): ?>
<?php echo strtoupper($_layoutType); ?>
<?php else: ?>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?layoutType=<?php echo $_layoutType; ?>"><?php echo strtoupper($_layoutType); ?></a>
<?php endif; ?>
<?php endforeach;