\n Please install and add entry to your php.ini file
\n"; } // List of all relevant HTTP headers $allHttpHeaders_in = $provider->getHttpHeaders(); for($i=0;$i<$allHttpHeaders_in->size();$i++) { $allHttpHeaders[$i] = $allHttpHeaders_in->get($i); } // Information about data set. // Determine if Pattern or Trie detector is used. $isPattern = True; $algorithmUsed = "Pattern"; if (strpos($provider->getDataSetName(), 'Trie') !== false) { $isPattern = FALSE; $algorithmUsed = "Trie"; } // List of properties as declared in PHP.ini file. // List may be different to properties contained in the initialised data set. // Some properties may not be in the data set you are trying to use. // I.e. Lite data set does not contain properties like IsTablet or DeviceType. if ($isPattern) { $iniProperties = ini_get('FiftyOneDegreesPatternV3.property_list'); $properties = explode(",", $iniProperties); } elseif ($algorithmUsed == "Trie") { $iniProperties = ini_get('FiftyOneDegreesTrieV3.property_list'); $properties = explode(",", $iniProperties); } if ($properties[1] == null) { $properties_in = $provider->getAvailableProperties(); for ($i=0;$i<$properties_in->size();$i++) { $properties[$i] = $properties_in->get($i); } } // 51Degrees Logo image link. $logoImage = getWebsiteLink("image", $isPattern); ?>

51Degrees logo image for PHP C Extension.

PHP - Device Detection Example

getMatch($_SERVER['HTTP_USER_AGENT']); printArrayToTable($match_ua, $properties, $isPattern, "ua"); printHttpHeaders($allHttpHeaders, "HTTP Headers"); $httpHeaders = ""; //Get all HTTP headers from $_SERVER foreach ($_SERVER as $key => $value) { if (strpos($key, "HTTP_") !== false) { $httpHeaders = $httpHeaders.$key." ".$value."\n"; } } $result_1 = $provider->getMatch($httpHeaders); printArrayToTable($result_1, $properties, $isPattern, 2); /** * Function prints the contents of a supplied array as a table. * @param $array is the array to print out. Mandatory. * @param $colNames a one dimensional array that contains names of columns. Optional. * Default values will be used if parameter not provided. */ function printArrayToTable($match, $properties, $isPattern, $occurance, $colNames=array("Property","Value","Data Type")) { // Columns in the array. $columnsInTable = count($colNames); // Extra column for the button. $columnsInArrayWithButton = $columnsInTable + 1; // Number of rows for properties. // Id, Rank, Method and Difference are not displayed in the properties // section, so we need less rows. // Default is for Trie and Trie only provides Id. $propertiesRows = count($properties) + 2; // Number of Match Metrics rows. Pattern provides more metrics stats. $metricsRowspan = 2; if ($isPattern) { // Pattern provides more metrics than Trie, hence more rows. $metricsRowspan = 5; } echo ""; echo ""; // Print metrics section. echo ""; echo ""; // Print 'About metrics' button. echo ""; echo ""; //Print device Id. echo ""; echo ""; if (NULL !== $match->getDeviceId()) { //For Trie Id is an optional property. May not necessarily be set. echo ""; echo ""; } else { echo ""; echo ""; } echo ""; if ($isPattern) { //Print detection method. echo ""; echo ""; echo ""; echo ""; echo ""; //Difference. echo ""; echo ""; echo ""; echo ""; echo ""; //Rank. echo ""; echo ""; echo ""; echo ""; echo ""; } //Print column names //Print Device properties header echo ""; echo ""; echo ""; echo ""; echo ""; foreach($colNames as $key) { echo ""; } echo ""; //Print table contents. if ($properties != NULL) { //Dataset was in fact initialised with a list of properties. foreach($properties as $value) { //Ignore Rank, Id, Method and Difference as they are printed elsewhere. if ($value === "SignatureRank" || $value === "Rank" || $value === "Difference" || $value === "Method" || $value ==="Id") { continue; } echo ""; echo ""; if ($match->getValue($value) != NULL) { echo ""; } else { echo ""; } echo ""; echo ""; } } else { //NULL was provided, meaning all properties selected. foreach($match->getAvailableProperties() as $key => $value) { //Ignore Rank, Id, Method and Difference as they are printed elsewhere. if ($key === "SignatureRank" || $key === "Rank" || $key === "Difference" || $key === "Method" || $key ==="Id") { continue; } echo ""; echo ""; echo ""; echo ""; echo ""; } } echo ""; echo "
Match Metrics"; $link = getWebsiteLink("metrics", $isPattern, $occurance); echo "About Metrics"; echo "
Id".$match->getDeviceId()."".gettype($match->getDeviceId())."N/AN/A
Method".$match->getMethod()."".gettype($match->getMethod())."
Difference".$match->getDifference()."".gettype($match->getDifference())."
Rank".$match->getRank()."".gettype($match->getRank())."
Device Properties"; $link = getWebsiteLink("properties", $isPattern, $occurance); echo "More Properties"; echo "
$key
$value".$match->getValue($value)."Switch Data Set".gettype($match->getValue($value))."
$key".$value."".gettype($value)."
"; } /** * Function prints information related to the data file currently in use. */ function printInfoTable($provider, $isPattern) { //How many columns will be required. $columns = 3; echo ""; echo ""; echo ""; echo ""; echo ""; //Print dataset initialisation message. //Third column spans all rows and is used to print 'Compare Data Options' button. if ($columns == 3) { echo ""; } echo ""; //Print dataset Published date. Pattern only. echo ""; echo ""; if ($isPattern) { echo ""; } else { echo ""; } echo ""; //Print dataset next update date. Pattern only. echo ""; echo ""; if ($isPattern) { echo ""; } else { echo ""; } echo ""; //Print dataset version. Pattern only. echo ""; echo ""; if ($isPattern) { echo ""; } else { echo ""; } echo ""; //Print dataset name. Exists for both Pattern and Trie. Both Pattern and Trie. echo ""; echo ""; echo ""; echo ""; //Print device combinations. Pattern only. echo ""; echo ""; if ($isPattern) { echo ""; } else { echo ""; } echo ""; echo ""; echo "
Data Set Information
"; $link = getWebsiteLink("compare", $isPattern); echo "Compare Data Options"; echo "
Dataset published:".$provider->getDataSetPublishedDate()."Not available for Trie.
Dataset next update:".$provider->getDataSetNextUpdateDate()."Not available for Trie.
Dataset version:".$provider->getDataSetFormat()."Not available for Trie.
Dataset name:".$provider->getDataSetName()."
Dataset device combinations:".$provider->getDataSetDeviceCombinations()."Not available for Trie.
"; } /** * Function prints all relevant headers and values for the relevant HTTP * headers that were set in this request. */ function printHttpHeaders($array, $name, $headers=array()) { if (empty($array)) { return null; } $selective = FALSE; if(!empty($headers)) { $selective = TRUE; } echo ""; echo ""; echo ""; echo ""; echo ""; foreach ($array as $value) { if ($selective) { //Only some headers are required. //If current header not in the array of required headers skip. if (!in_array("HTTP_".strtoupper(str_replace("-","_",$value)), $headers)) { continue; } } echo ""; echo ""; echo ""; echo ""; } echo ""; echo "
Match With $name
"; echo $value; echo ""; if ($_SERVER["HTTP_".strtoupper(str_replace("-","_",$value))] != NULL) { echo $_SERVER["HTTP_".strtoupper(str_replace("-","_",$value))]; } else { echo "header not set"; } echo "
"; } /** * Function generates a link to 51Degrees based on the purpose of the link, whether * the detector is operating in Pattern or Trie modes and if the link is created for * the match with User-Agent function or the Match with Headers function. Guaranteed * to return a link, even if the parameter is not known. * @returns a link to 51Degrees.com */ function getWebsiteLink($purpose, $isPattern, $occurance=1) { $url = "https://51degrees.com"; $utmSource = "utm_source=Github"; $utmMedium = "utm_medium=repository"; $utmContent = "example_trie"; if($isPattern) { $utmContent = "example_pattern"; } $utmCampaign = "utm_campaign=php-open-source"; if ($purpose === 'image') { $url = "https://51degrees.com/DesktopModules/FiftyOne/Distributor/Logo.ashx"; } else if ($purpose === 'metrics') { $url = "https://51degrees.com/support/documentation/howdevicedetectionworks/trie"; if ($isPattern) { $url = "https://51degrees.com/support/documentation/pattern"; } if ($occurance == 2) { $utmContent .= "_header"; } else { $utmContent .= "_ua"; } } else if ($purpose === 'compare') { $url = "https://51degrees.com/compare-data-options"; $utmContent .= "_compare"; } else if ($purpose === 'properties') { $url = "https://51degrees.com/resources/property-dictionary"; if ($occurance == 2) { $utmContent .= "_properties_header"; } else { $utmContent .= "_properties_ua"; } } return $url."?".$utmSource."&".$utmMedium."&".$utmContent."&".$utmCampaign; } // Snippet End ?>