This example demonstrates device detection using User-Agent Client Hints on a website.
This example demonstrates device detection using User-Agent Client Hints on a website. When a browser that supports User-Agent Client Hints first visits a website, the server receives the sec-ch-ua and sec-ch-ua-mobile headers along with the user-agent header. The server uses these headers to determine whether the browser supports User-Agent Client Hints. It can request additional hints by setting the Accept-CH response header. Clicking the button below will cause a new request to be sent to the server, along with any additional headers that have been requested.
This example shows how a simple device detection pipeline can be built that checks if a provided User-Agent is a mobile device
<?php
require(__DIR__ . "/../../vendor/autoload.php");
$resourceKey = ExampleUtils::getResourceKeyFromEnv();
if (empty($resourceKey)) {
$resourceKey = ExampleUtils::getResourceKeyFromQueryParameter();
}
if (empty($resourceKey)) {
$message = 'No resource key specified in the environment variable or query parameter ';
$message .= "'" . ExampleUtils::RESOURCE_KEY_ENV_VAR . "'." . PHP_EOL;
$message .= 'Create a resource key with the properties required by this example';
$message .= 'at https://configure.51degrees.com?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-cloud-useragentclienthints-web.php&utm_term=resource-key-required' . '<br/>';
$message .= 'Once complete, populate the environment variable or query parameter ';
$message .= 'mentioned at the start of this message with the key.' . '<br/>';
ExampleUtils::logErrorAndExit(new Logger('info'), $message);
}
$builder = new DeviceDetectionPipelineBuilder(array(
"resourceKey" => $resourceKey,
"suppressProcessExceptions" => true
));
$pipeline = $builder->build();
$flowData = $pipeline->createFlowData();
$flowData->evidence->setFromWebRequest();
$flowData->process();
$device = $flowData->device;
Utils::setResponseHeader($flowData);
?>
<head>
<title>User Agent Client Hints Example</title>
<style>
<?php
require(__DIR__ . "/static/examples-main.min.css");
?>
</style>
</head>
<div class="c-eg-page">
<h2 class="c-eg-page__title">User Agent Client Hints example</h2>
<p class="c-eg-page__lead">
By default, the user-agent, sec-ch-ua and sec-ch-ua-mobile HTTP headers are sent.
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.
</p>
<p class="c-eg-page__lead">
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.
Select the <strong>Make second request</strong> button below to send another request
to the server. This time, any additional client hints headers that have been requested
will be included.
</p>
<div class="c-eg-button-row">
<button type="button" class="b-btn" onclick="redirect()">Make second request</button>
</div>
<script>
function redirect() {
sessionStorage.reloadAfterPageLoad = true;
window.location.reload(true);
}
window.onload = function () {
if (sessionStorage.reloadAfterPageLoad) {
document.getElementById('description').innerHTML = '<p class="c-eg-page__lead">The information shown below is determined using <strong>User Agent Client Hints</strong> that were 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 class="c-eg-page__lead">The following values are determined by server-side device detection on the first request.</p>';
}
}
</script>
<div id="evidence">
<h3 class="c-eg-page__heading">Evidence values used</h3>
<p class="c-eg-legend">
Evidence was
<span class="c-eg-legend__swatch c-eg-legend__swatch--used">used</span>
/
<span class="c-eg-legend__swatch c-eg-legend__swatch--present">present</span>
for detection
</p>
<table class="c-eg-table">
<thead class="c-eg-table__head">
<tr class="c-eg-table__row">
<th class="c-eg-table__cell">Key</th>
<th class="c-eg-table__cell">Value</th>
</tr>
</thead>
<tbody>
<?php
$evidences = $pipeline->getElement("device")->filterEvidence($flowData);
foreach ($evidences as $key => $value) {
if (strpos($key, strtolower("header.sec-ch")) !== false
|| strpos($key, strtolower("header.user-agent")) !== false) {
echo "<tr class='c-eg-table__row c-eg-table__row--used'>";
echo "<td class='c-eg-table__cell c-eg-table__cell--key'>" . strVal($key) . "</td>";
echo "<td class='c-eg-table__cell'>" . strVal($value) . "</td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
</div>
<h3 class="c-eg-page__heading">Detection results</h3>
<div id="description"></div>
<div id="content">
<table class="c-eg-table">
<thead class="c-eg-table__head">
<tr class="c-eg-table__row">
<th class="c-eg-table__cell">Key</th>
<th class="c-eg-table__cell">Value</th>
</tr>
</thead>
<tbody>
<tr class="c-eg-table__row c-eg-table__row--alt"><td class="c-eg-table__cell c-eg-table__cell--key">Hardware Vendor:</td><td class="c-eg-table__cell"><?php echo $device->hardwarevendor->hasValue ? $device->hardwarevendor->value : $device->hardwarevendor->noValueMessage; ?></td></tr>
<tr class="c-eg-table__row"><td class="c-eg-table__cell c-eg-table__cell--key">Hardware Name:</td><td class="c-eg-table__cell"><?php echo $device->hardwarename->hasValue ? implode(", ", $device->hardwarename->value) : $device->hardwarename->noValueMessage; ?></td></tr>
<tr class="c-eg-table__row c-eg-table__row--alt"><td class="c-eg-table__cell c-eg-table__cell--key">Device Type:</td><td class="c-eg-table__cell"><?php echo $device->devicetype->hasValue ? $device->devicetype->value : $device->devicetype->noValueMessage; ?></td></tr>
<tr class="c-eg-table__row"><td class="c-eg-table__cell c-eg-table__cell--key">Platform Vendor:</td><td class="c-eg-table__cell"><?php echo $device->platformvendor->hasValue ? $device->platformvendor->value : $device->platformvendor->noValueMessage; ?></td></tr>
<tr class="c-eg-table__row c-eg-table__row--alt"><td class="c-eg-table__cell c-eg-table__cell--key">Platform Name:</td><td class="c-eg-table__cell"><?php echo $device->platformname->hasValue ? $device->platformname->value : $device->platformname->noValueMessage; ?></td></tr>
<tr class="c-eg-table__row"><td class="c-eg-table__cell c-eg-table__cell--key">Platform Version:</td><td class="c-eg-table__cell"><?php echo $device->platformversion->hasValue ? $device->platformversion->value : $device->platformversion->noValueMessage; ?></td></tr>
<tr class="c-eg-table__row c-eg-table__row--alt"><td class="c-eg-table__cell c-eg-table__cell--key">Browser Vendor:</td><td class="c-eg-table__cell"><?php echo $device->browservendor->hasValue ? $device->browservendor->value : $device->browservendor->noValueMessage; ?></td></tr>
<tr class="c-eg-table__row"><td class="c-eg-table__cell c-eg-table__cell--key">Browser Name:</td><td class="c-eg-table__cell"><?php echo $device->browsername->hasValue ? $device->browsername->value : $device->browsername->noValueMessage; ?></td></tr>
<tr class="c-eg-table__row c-eg-table__row--alt"><td class="c-eg-table__cell c-eg-table__cell--key">Browser Version:</td><td class="c-eg-table__cell"><?php echo $device->browserversion->hasValue ? $device->browserversion->value : $device->browserversion->noValueMessage; ?></td></tr>
</tbody>
</table>
</div>
<?php
echo '<div class="c-eg-message">';
echo ' <p class="c-eg-message__text">Want to try on-premise? <a href="/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-cloud-useragentclienthints-web.php&utm_term=try-on-premise">Contact us</a> to discuss requirements.</p>';
echo ' <a class="b-btn c-eg-message__cta" href="/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-cloud-useragentclienthints-web.php&utm_term=try-on-premise">Contact us</a>';
echo '</div>';
?>
</div>
Definition ExampleUtils.php:29