This example demonstrates device detection using user-agent client hints in a real-world scenario. When a clint-hints-enabled user first visits a website, the server will recieve the sec-ch-ua and sec-ch-ua-mobile headers. Along with the user-agent. This information is then used to determine if the client supports user-agent client hints and request them if needed by setting the Accept-CH header in the response. Clicking the button below will cause a new request to be sent to the server, along with any additional headers that have been requested.
In this example we create an on premise 51Degrees device detection pipeline, in order to do this you will need a copy of the 51Degrees on-premise library and need to make the following additions to your php.ini file
When running under process manager such as Apache MPM or php-fpm, make sure to set performance_profile to MaxPerformance by making the following addition to php.ini file. More details can be found in README.
<?php
require __DIR__ . '/../../vendor/autoload.php';
use fiftyone\pipeline\core\PipelineBuilder;
use fiftyone\pipeline\core\Utils;
$device = new DeviceDetectionOnPremise();
$builder = new PipelineBuilder([]);
$pipeline = $builder->add($device)->build();
$flowData = $pipeline->createFlowData();
$flowData->evidence->setFromWebRequest();
$flowData->process();
$device = $flowData->device;
Utils::setResponseHeader($flowData);
echo '<h2>User Agent Client Hints Example</h2>';
echo "
<p>
By default, the user-agent, sec-ch-ua and sec-ch-ua-mobile HTTP headers
are sent.
<br />
This means that on the first request, the server can determine the
browser from sec-ch-ua while other details must be derived from the
user-agent.
<br />
If the server determines that the browser supports client hints, then
it may request additional client hints headers by setting the
Accept-CH header in the response.
<br />
Select the <strong>Make second request</strong> button below,
to use send another request to the server. This time, any
additional client hints headers that have been requested
will be included.
</p>
<button type='button' onclick='redirect()'>Make second request</button>
<script>
// This script will run when button will be clicked and device detection request will again
// be sent to the server with all additional client hints that was requested in the previous
// response by the server.
// Following sequence will be followed.
// 1. User will send the first request to the web server for detection.
// 2. Web Server will return the properties in response based on the headers sent in the request. Along
// with the properties, it will also send a new header field Accept-CH in response indicating the additional
// evidence it needs. It builds the new response header using SetHeader[Component name]Accept-CH properties
// where Component Name is the name of the component for which properties are required.
// 3. When \"Make second request\" button will be clicked, device detection request will again
// be sent to the server with all additional client hints that was requested in the previous
// response by the server.
// 4. Web Server will return the properties based on the new User Agent CLient Hint headers
// being used as evidence.
function redirect() {
sessionStorage.reloadAfterPageLoad = true;
window.location.reload(true);
}
window.onload = function () {
if ( sessionStorage.reloadAfterPageLoad ) {
document.getElementById('description').innerHTML = '<p>The information shown below is determined using <strong>User Agent Client Hints</strong> that was sent in the request to obtain additional evidence. If no additional information appears then it may indicate an external problem such as <strong>User Agent Client Hints</strong> being disabled in your browser.</p>';
sessionStorage.reloadAfterPageLoad = false;
}
else{
document.getElementById('description').innerHTML = '<p>The following values are determined by sever-side device detection on the first request.</p>';
}
}
</script>
<div id=\"evidence\">
<strong></br>Evidence values used: </strong>
<table>
<tr>
<th>Key</th>
<th>Value</th>
</tr>";
$evidences = $pipeline->getElement('device')->filterEvidence($flowData);
foreach ($evidences as $key => $value) {
echo '<tr><td>' . strval($key) . '</td>';
echo '<td>' . strval($value) . '</td></>';
}
echo '</table>';
echo '</div>';
echo '<div id=description></div>';
echo '</br><strong>Detection results:</strong></br>';
echo '<div id="content">';
echo "<p>\n";
echo ' Hardware Vendor: ' . (isset($device->hardwarevendor) && $device->hardwarevendor->hasValue ? $device->hardwarevendor->value : 'Unknown (' . (isset($device->hardwarevendor) ? $device->hardwarevendor->noValueMessage : 'property unavailable') . ')') . "<br />\n";
echo ' Hardware Name: ' . (isset($device->hardwarename) && $device->hardwarename->hasValue ? implode(',', $device->hardwarename->value) : 'Unknown (' . (isset($device->hardwarename) ? $device->hardwarename->noValueMessage : 'property unavailable') . ')') . "<br />\n";
echo ' Device Type: ' . (isset($device->devicetype) && $device->devicetype->hasValue ? $device->devicetype->value : 'Unknown (' . (isset($device->devicetype) ? $device->devicetype->noValueMessage : 'property unavailable') . ')') . "<br />\n";
echo ' Platform Vendor: ' . (isset($device->platformvendor) && $device->platformvendor->hasValue ? $device->platformvendor->value : 'Unknown (' . (isset($device->platformvendor) ? $device->platformvendor->noValueMessage : 'property unavailable') . ')') . "<br />\n";
echo ' Platform Name: ' . (isset($device->platformname) && $device->platformname->hasValue ? $device->platformname->value : 'Unknown (' . (isset($device->platformname) ? $device->platformname->noValueMessage : 'property unavailable') . ')') . "<br />\n";
echo ' Platform Version: ' . (isset($device->platformversion) && $device->platformversion->hasValue ? $device->platformversion->value : 'Unknown (' . (isset($device->platformversion) ? $device->platformversion->noValueMessage : 'property unavailable') . ')') . "<br />\n";
echo ' Browser Vendor: ' . (isset($device->browservendor) && $device->browservendor->hasValue ? $device->browservendor->value : 'Unknown (' . (isset($device->browservendor) ? $device->browservendor->noValueMessage : 'property unavailable') . ')') . "<br />\n";
echo ' Browser Name: ' . (isset($device->browsername) && $device->browsername->hasValue ? $device->browsername->value : 'Unknown (' . (isset($device->browsername) ? $device->browsername->noValueMessage : 'property unavailable') . ')') . "<br />\n";
echo ' Browser Version: ' . (isset($device->browserversion) && $device->browserversion->hasValue ? $device->browserversion->value : 'Unknown (' . (isset($device->browserversion) ? $device->browserversion->noValueMessage : 'property unavailable') . ')') . "\n";