I try to set device detection as universal detection script. How to manage PHP script as it will be an error: Uncaught Error: Class ‘BotParser’
init script
I have checked again.
An error:
PHP Fatal error: Uncaught TypeError: Argument 1 passed to DeviceDetector\Parser\AbstractParser::setUserAgent() must be of the type string, null given, called in matomo/device-detector/Parser/AbstractParser.php:145
//use DeviceDetector\DeviceDetector;
//use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Parser\Bot AS BotParser;
Well no, the error is telling you that the method DeviceDetector\Parser\AbstractParser::setUserAgent() expects a string, but is given null instead.
So what that means is that somehow $_SERVER['HTTP_USER_AGENT'] is null. Which led me to believe you might be testing this on the CLI instead of in a browser. Hence the question of how you are testing this
If I understand we need to push also other library and this was mistake due to avoided
//use DeviceDetector\DeviceDetector;
//use DeviceDetector\Parser\Device\AbstractDeviceParser;
require_once 'vendor/autoload.php';
$userAgent = $_SERVER[‘HTTP_USER_AGENT’];
use DeviceDetector\Parser\Bot AS BotParser;
$botParser = new BotParser();
$botParser->setUserAgent($userAgent);
// OPTIONAL: discard bot information. parse() will then return true instead of information
$botParser->discardDetails();
$result = $botParser->parse();
if (!is_null($result)) {
// do not do anything if a bot is detected
return;
}
// handle non-bot requests
How to handle non-bot requests?
Is it correct the bottom code?
You might get more feedback if you read the questions you were asked and answer them clearly. As it is, it looks like you are ignoring the advice being given.
Sorry if I posted the whole code and it is not clear. There are many factors which influence device detection. Maybe working code at https://codepen.io/ could be better.
I have checked again from previous responses and help, there is also a command which prevents
// handle non-bot requests :
return;
Currently, I do not know what is wrong. I have added a comment and also library to read devices: