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