Well these arrays are going to be assigned to a property of the object. So you will need to find that name and use it… $clientInfo->some_name['engine'] and that will equal Gecko.
Now if these arrays are together in an array then they both will be under some property name and could be referenced like… $clientInfo->some_name[1]['name'] and that would give you ‘Mac’
You might want to show us the entire object’s print_r or var_dump output if you are still having trouble locating the correct property name.
require_once ROOT_DIR . "vendor/autoload.php";
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Parser\Bot AS BotParser;
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$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
$dd = new DeviceDetector($userAgent);
$dd->parse();
$clientInfo = $dd->getClient();
$osInfo = $dd->getOs();
$device = $dd->getDeviceName();
$brand = $dd->getBrandName();
$model = $dd->getModel();
//Objects with arrays!
//var_dump($clientInfo);
//var_dump($osInfo);
//var_dump($device);
//var_dump($brand);
//var_dump($model);
$clientInfo->some_name['engine'];
$clientInfo->some_name[1]['name'];
$osInfo->some_name[1]['name'];
It does not show up specifically value into variables even objects and arrays are known. I have an issue how to store clientInfo into PHP variable specifically engine or osInfo specifically
clientInfo is already a PHP variable. $clientInfo["engine"] is Gecko, in your example.
you already know how to do assignment statements, because you used one to assign the result of getClient to $clientInfo.
I border with the last point. If I use usual device (not a bot) it will start the first IF sentence
As I understand $botParser->discardDetails(); should start the first IF sentence. Is it the correct as my value should show only on the second if and the first IF should EXIT. Need help if I’m wrong.
I don’t know what your functions do.
discardDetails makes it sound like its throwing things away;
then you parse, but dont tell it what to parse, so i assume it knows what to go and do.
then you check to see if $result is null.
Generally speaking, i’d expect parse to return SOMETHING, even if it’s a false. false != null.
/**
* Sets whether to discard additional bot information
* If information is discarded it's only possible check whether UA was detected as bot or not.
* (Discarding information speeds up the detection a bit)
*
* @param bool $discard
*/
public function discardBotInformation(bool $discard = true): void
{
$this->discardBotInformation = $discard;
}
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;
}
An example:
Mozilla/5.0 (compatible; SemrushBot/7~bl; +http://www.semrush.com/bot.html)
(United States,US)
So, bot should be detected and blocked inside the first IF sentence. Our goal is bot detection and block any contact form submissions.