The module can also handling setting response headers (e.g. Accept-CH for User-Agent Client Hints) and serving requests for client-side JavaScript and JSON resources.
The results of detection can be accessed by through the flowdata object once processed. This can then be used to interrogate the data.
<?php
?>
<head>
<title>Web Integration Example</title>
<link rel="stylesheet" href="/css/examples-main.min.css" />
</head>
<div class="c-eg-page">
<?php if (ExampleUtils::dataFileIsOld($flowData->pipeline->getElement('device'))) { ?>
<div class="c-eg-alert">
WARNING: This example is using a data file that is more than
<?php echo ExampleUtils::DATA_FILE_AGE_WARNING; ?>
days old. A more recent data file may be needed to
correctly detect the latest devices, browsers, etc. Get the latest lite data file by
running <code>cxx/data/get-data-files.sh -u 51Degrees-LiteV4.1.hash</code>, or
<code>cxx/data/get-data-files.ps1 51Degrees-LiteV4.1.hash -Update</code> on Windows.
The scripts are in the <a href="https://github.com/51Degrees/data">data</a>
repository on GitHub. <a href="/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-onpremise-static-page.php&utm_term=data-file-age-warning">Contact us</a>
about the Enterprise data file, which includes automatic daily updates.
</div>
<?php } ?>
<h2 class="c-eg-page__title">Web integration example</h2>
<p class="c-eg-page__lead">
This example demonstrates the use of the Pipeline API to perform device detection within a
simple PHP web project. In particular, it highlights:
</p>
<ol>
<li>
Automatic handling of the 'Accept-CH' header, which is used to request User-Agent
Client Hints from the browser.
</li>
<li>
Client-side evidence collection in order to identify Apple device models and properties
such as screen size.
</li>
</ol>
<h3 class="c-eg-page__heading">Client hints</h3>
<p class="c-eg-page__lead">
When the first request is made, browsers that support client hints will typically send a subset
of client hints values along with the User-Agent header.
If device detection determines that the browser does support client hints then it will request
that additional client hints headers are sent with future requests by sending the Accept-CH
header with the response.
</p>
<p class="c-eg-page__lead">
Note that if you have visited this page previously, the value of Accept-CH will have been
cached so all requested client hints headers will be sent on the first request. Using features
such as 'private browsing' or 'incognito mode' will allow you to see the true first request
experience as the previous Accept-CH value will not be used.
</p>
<noscript>
<div class="c-eg-alert">
WARNING: JavaScript is disabled in your browser. This means that the callback discussed
further down this page will not fire and UACH headers will not be sent.
</div>
</noscript>
<div id="content">
<div id="response-headers">
<h3 class="c-eg-page__heading">Response headers</h3>
<p class="c-eg-page__lead">The following response headers were set:</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
foreach (headers_list() as $header) {
$parts = explode(': ', $header);
$output("<tr class='c-eg-table__row c-eg-table__row--present'>");
$output('<td class="c-eg-table__cell c-eg-table__cell--key">' . $parts[0] . '</td>');
$output('<td class="c-eg-table__cell">' . $parts[1] . '</td>');
$output('</tr>');
}
?>
</tbody>
</table>
</div>
<?php if (ExampleUtils::containsAcceptCh() == false) { ?>
<div class="c-eg-alert">
WARNING: There is no Accept-CH header in the response. This may indicate that your
browser does not support User-Agent Client Hints. This is not necessarily a problem,
but if you are wanting to try out detection using User-Agent Client Hints, then make
sure that your browser
<a href="https://developer.mozilla.org/en-US/docs/Web/API/User-Agent_Client_Hints_API#browser_compatibility">supports them</a>.
</div>
<?php } ?>
<div id="evidence">
<h3 class="c-eg-page__heading">Evidence 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
foreach ($flowData->evidence->getAll() as $key => $value) {
if ($flowData->pipeline->getElement('device')->getEvidenceKeyFilter()->filterEvidenceKey($key)) {
$output("<tr class='c-eg-table__row c-eg-table__row--used'>");
} else {
$output("<tr class='c-eg-table__row c-eg-table__row--present'>");
}
$output('<td class="c-eg-table__cell c-eg-table__cell--key">' . $key . '</td>');
$output('<td class="c-eg-table__cell">' . $value . '</td>');
$output('</tr>');
}
?>
</tbody>
</table>
</div>
<h3 class="c-eg-page__heading">Device data</h3>
<p class="c-eg-page__lead">
The following values are determined by server-side device detection
on the first request:
</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>
<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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'hardwarevendor')); ?></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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'hardwarename')); ?></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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'devicetype')); ?></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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'platformvendor')); ?></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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'platformname')); ?></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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'platformversion')); ?></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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'browservendor')); ?></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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'browsername')); ?></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 $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'browserversion')); ?></td></tr>
<tr class="c-eg-table__row"><td class="c-eg-table__cell c-eg-table__cell--key">Screen width (pixels):</td><td class="c-eg-table__cell"><?php $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'screenpixelswidth')); ?></td></tr>
<tr class="c-eg-table__row c-eg-table__row--alt"><td class="c-eg-table__cell c-eg-table__cell--key">Screen height (pixels):</td><td class="c-eg-table__cell"><?php $output(ExampleUtils::getEnterpriseValue($flowData->pipeline->getElement('device'), $flowData->device, 'screenpixelsheight')); ?></td></tr>
<tr class="c-eg-table__row"><td class="c-eg-table__cell c-eg-table__cell--key">Device Id:</td><td class="c-eg-table__cell"><?php $output(ExampleUtils::getHumanReadable($flowData->device, 'deviceid')); ?></td></tr>
</tbody>
</table>
<h3 class="c-eg-page__heading">Client-side evidence and Apple models</h3>
<p class="c-eg-page__lead">
The information shown below is determined after a callback is made to the server with
additional evidence that is gathered by JavaScript running on the client-side.
The callback will also include any additional client hints headers that have been requested.
</p>
<p class="c-eg-page__lead">
When an Apple device is used, the results from
the first request above will show all Apple models because the server cannot tell the
exact model of the device. In contrast, the results from the callback below will show
a smaller set of possible models.
This can be tested to some extent using most emulators, such as those in the
'developer tools' menu in Google Chrome. However, these are not the identical to real
devices so this can cause some unusual results. Using real devices will generally be more
successful.
</p>
<p class="c-eg-page__lead">
If you want to work with Apple Model or other client-side information, such as screen
width/height on the server, then you will need to ensure that the 'enableCookies' setting
is set to 'true' as in the pipeline construction for this example.
This will cause the additional client-side evidence to be saved as cookies on the client.
When a future page is requested, these cookies will be included with the request and the
device detection API will include them when working out the details of the device.
Refreshing this page can be used to show this in action. Any values that are unique to the
client-side values below will appear in the evidence values used and server-side results
after the refresh.
</p>
</div>
<?php $showContactUs = ExampleUtils::getDataFileTier($flowData->pipeline->getElement('device')) === 'Lite'; ?>
<?php if ($showContactUs) { ?>
<?php $output('<div class="c-eg-message">'); ?>
<?php $output(' <p class="c-eg-message__text">The paid data file adds daily automatic updates, non-human identification and IP intelligence. <a href="/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-onpremise-static-page.php&utm_term=paid-data-file">Contact us</a> to explore the options.</p>'); ?>
<?php $output(' <a class="b-btn c-eg-message__cta" href="/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-onpremise-static-page.php&utm_term=paid-data-file">Contact us</a>'); ?>
<?php $output('</div>'); ?>
<?php } ?>
<div id="find-out-more">
<h3 class="c-eg-page__heading">Find out more</h3>
<ul>
<li>This example: https:
<li>The device detection engine: https:
<li>The free Lite data file: https:
<li><a href="/pricing?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-onpremise-static-page.php&utm_term=find-out-more-pricing">Data files and pricing</a></li>
<li><a href="/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-onpremise-static-page.php&utm_term=find-out-more-contact">Contact us</a></li>
</ul>
</div>
</div>
<!--
51Degrees.core.js is served by this example from the same script that
renders this page, see GettingStartedWeb::processRequest.
The 51Degrees pipeline will dynamically generate JavaScript, which includes a
JSON representation of the contents of flow data.
i.e. The results from device detection.
In addition, this JavaScript will look for properties that have a flag set indicating that
they contain executable script snippets.
These snippets will be executed and the values they obtain will be sent back to the server
in order for it to perform the detection process again with the new information.
This callback will also include any User-Agent Client Hints headers that have been requested
with the 'Accept-CH' header. (assuming the browser is willing to send them)
When the server responds, the JSON representation of the results will be updated with the
new values and the 'complete' event will fire. The shared examples.js helper subscribes to
that event and appends a results table into #content.
-->
<script src="/51Degrees.core.js"></script>
<script src="/js/examples.min.js"></script>
<script>
window.onload = function () {
fodExamples.bindDeviceCallback({ targetId: "content" });
};
</script>
Definition ExampleUtils.php:27