This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
109 lines (73 loc) · 4.06 KB
/
index.php
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
<?php
require('gbs.php');
?><!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="utf-8" />
<title>Server-side detection of Yahoo Graded Browser Support C-grade browsers</title>
<?php
if(!isset($gbs) || $gbs != 'C-grade') {
?>
<link rel="stylesheet" href="gbs.css" />
<?php
}
?>
</head>
<body>
<div id="container">
<h1>Graded Browser Support</h1>
<p><a href="https://github.com/monospaced/Graded-Browser-Support">github.com/monospaced/Graded-Browser-Support</a></p>
<div class="line-length">
<h2>Server-side detection of Yahoo <abbr title="Graded Browser Support">GBS</abbr> C-grade browsers</h2>
<small>21/12/2010</small>
<?php
if(isset($gbs)) {
echo '<p><strong>Your browser receives ', $gbs,' support.</strong></p>';
}
?>
<p>In the main, <a href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support</a> is a <abbr title="Quality Assurance">QA</abbr> strategy that can be applied to any website, without the need for specific functionality to be present in the site itself. However, the one thing the site <em>does</em> need to do, is detect browsers on the <a href="http://developer.yahoo.com/yui/articles/gbs/#cgradelist">C-Grade blacklist</a> so that <abbr title="Cascading Style Sheets">CSS</abbr> and JavaScript can be withheld.</p>
<p>I′ve often wondered exactly how Yahoo implements this C-Grade detection. They do mention that it's done on the server-side, which makes sense, but there seemed to be no further information online…</p>
<p>… until a few days ago, when I came across a <a href="http://tech.groups.yahoo.com/group/ydn-javascript/message/20753">post on ydn-javascript</a> by Nate Koechley, with this snippet right at the very end:</p>
<blockquote><p>“A final note: for what it's worth, internally we use the <abbr>PHP</abbr> version of Browscap.ini to do detection and grading at the server level. <a href="http://browsers.garykeith.com/downloads.asp">http://browsers.garykeith.com/downloads.asp</a>”</p></blockquote>
<p>Now I was on the right track, and with the help of Evan Byrne's <a href="http://www.evanbyrne.com/article/php-browscap">Browser Detection With <abbr>PHP</abbr> Browscap</a> tutorial (using Jonathan Stoppani's <a href="https://github.com/garetjax/phpbrowscap">phpbrowscap</a>) I had <abbr>PHP</abbr> browser detection up and running in no time.</p>
<p>With the Browscap data available, it was now relatively simple to implement the C-Grade blacklist in <abbr>PHP</abbr>. The following code did the trick, entered in a new file called gbs.php (created within the same folder as Browscap.php):</p>
<pre><code><?php
$gbs = 'A-grade or X-grade';
if (isset($_COOKIE['gbs'])) {
$gbs = $_COOKIE['gbs'];
} else {
require('Browscap.php');
$bc = new Browscap('cache');
$bc->localFile = 'php_browscap.ini';
$data = $bc->getBrowser();
$browser = $data->Browser;
$version = $data->Version;
if ($browser == 'IE' && $version < 6 ||
$browser == 'Safari' && $version < 3 ||
$browser == 'Firefox' && $version < 3 ||
$browser == 'Opera' && $version < 9.5 ||
$browser == 'Netscape' && $version < 8) {
$gbs = 'C-grade';
}
setcookie('gbs',$gbs);
}
?>
</code></pre>
<p>Pages can then includes this script, and uses the returned $gbs variable to determine whether to serve <abbr>CSS</Thabbr> and JavaScript. For example:</p>
<pre><code><?php
require('gbs.php');
?><!DOCTYPE html>
<html lang="en">
<head>
...
<?php if(!isset($gbs) || $gbs != 'C-grade') { ?>
<link rel="stylesheet" href="gbs.css" />
<?php } ?>
...
</code></pre>
<p>For a first iteration I'm pretty happy with this technique, but I'm sure it could be improved. The <a href="https://github.com/monospaced/Graded-Browser-Support">source is on github</a> so fork away!</p>
<p><a href="http://monospaced.github.com">Monospaced Labs</a></p>
</div>
</div>
</body>
</html>