This example shows how the hasValue function can help make sure that meaningful values are returned when checking properties returned from the device detection engine.
This example shows how the hasValue function can help make sure that meaningful values are returned when checking properties returned from the device detection engine. This example is available in full on GitHub.
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
Definition FiftyOneDegreesHashEngine.php:29
When running under a process manager such as Apache MPM or php-fpm, the engine must load the whole data file into memory. This is now enforced automatically (the MaxPerformance profile is always used), so no extra configuration is required. More details can be found in README.
Expected output:
Does User-Agent 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114' represent a mobile device?:
Yes
Does User-Agent 'nonsense ua...' represent a mobile device?:
We don't know for sure. The reason given is:
The results contained a null profile for the component which the required property belongs to.
<?php
require __DIR__ . '/../../vendor/autoload.php';
$device = new DeviceDetectionOnPremise();
$pipeline = new PipelineBuilder();
$pipeline = $pipeline->add($device)->build();
function failuretomatch_checkifmobile($pipeline, $userAgent = '')
{
$flowData = $pipeline->createFlowData();
try {
$flowData->evidence->set('header.user-agent', $userAgent);
$result = $flowData->process();
echo "Does User-Agent '<b>" . $userAgent . "</b>' represent a mobile device?:";
echo "</br>\n";
if ($result->device->ismobile->hasValue) {
if ($result->device->ismobile->value) {
echo 'Yes';
} else {
echo 'No';
}
} else {
echo "We don't know for sure. The reason given is:";
echo "</br>\n";
echo $result->device->ismobile->noValueMessage;
echo "</br>\n";
}
echo "</br>\n";
echo "</br>\n";
} finally {
$flowData->close();
}
}
try {
$iPhoneUA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114';
failuretomatch_checkifmobile($pipeline, $iPhoneUA);
$badUserAgent = 'nonsense ua...';
failuretomatch_checkifmobile($pipeline, $badUserAgent);
} finally {
$pipeline->close();
}