I have tried to use Matomo GitHub script to detect device properties.
When I publish it is an error message:
DeviceDetector\Parser\AbstractParser->setUserAgent(NULL)
Use of undefined constant ‘HTTP_USER_AGENT’ - assumed ‘‘HTTP_USER_AGENT’’ (this will throw an Error in a future version of PHP).
Need help what is wrong with the code as it will not return device properties. Code is the following:
require_once ROOT_DIR . "vendor/autoload.php";
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Parser\Bot AS BotParser;
If you look at how the syntax highlighting of the forum colors the above line, you can already see the issue you are having: You are using typographical quotes instead of ’ or " to quote the HTTP_USER_AGENT which makes it invalid PHP code.
It seems it retrieves values for a client site under your valued suggestion: var_dump($clientInfo);. But why is not returned
OS info: ArrayDevice info: desktopBrand info: Model:
Is this composer library problem?
So, bot should be detected and blocked inside the first IF sentence. Our goal is bot detection and block any contact form submissions.
An example:
Mozilla/5.0 (compatible; SemrushBot/7~bl; +http://www.semrush.com/bot.html)
(United States,US)
Is this the correct?
if (!is_null($result)) {
// do not do anything if a bot is detected
return;
}
else {
$clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ...
$osInfo = $dd->getOs();
$device = $dd->getDevice();
$brand = $dd->getBrandName();
$model = $dd->getModel();
echo $model;
echo $device;
}
No, if you want to know if DeviceDetector considers a request a bot, you have to check for $dd->isBot() or in your case $result->isBot().
Also keep in mind that DeviceDetector can only detect bots that explicitly mention that they are a bot and the ones spamming your contact form most likely won’t do that.