在PHP中检测设备时出现未定义的属性错误

时间:2017-10-22 08:16:03

标签: php

我正在创建一个检测设备的页面。我使用Detect Device进行检测。但是我收到了一个错误:

enter image description here

我的 Index.php

<?php include 'detect.php'; 

if (Detect::isMobile()) {

}

// Gets the device type ('Computer', 'Phone' or 'Tablet').
echo Detect::deviceType();

// Any phone device
if (Detect::isPhone()) {

}

// Any tablet device.
if (Detect::isTablet()) {

}

// Any computer device (desktops or laptops).
if (Detect::isComputer()) {

}

// Get the IP address of the device.
echo Detect::ip();

// Get the ID address host name of the device.
echo Detect::ipHostname();

// Get the IP address organisation of the device.
echo Detect::ipOrg();

// Get the country the IP address is in (IP address location inaccurate).
// (JS function available which uses GPS)
echo Detect::ipCountry();

// Get the name & version of operating system.
echo Detect::os();

// Get the name & version of browser.
echo Detect::browser();

// Get the brand of device (only works with mobile devices otherwise return null).
echo Detect::brand();

// Check for a specific platform with the help of the magic methods:
if (Detect::isiOS()) {

}

if (Detect::isAndroidOS()) {

}
?>
<head>
    ...
    <script type="text/javascript" src="path_to/detect.js"></script>
    ...
</head>
<script>
// Get screen width in pixels.
detect.screenWidth();

// Get screen height in pixels.
detect.screenHeight();

// Get viewport (browser window minus any toolbars etc) width in pixels.
detect.viewportWidth();

// Get viewport (browser window minus any toolbars etc) height in pixels.
detect.viewportHeight();

// Get latitude from GPS & update html conent of ID element passed.
// Null, if GPS unavailable.
detect.latitude("latitude");

// Get longitude from GPS & update html conent of ID element passed.
// Null, if GPS unavailable.
detect.longitude("longitude");

// Get address from GPS & update html conent of ID element passed.
// Null, if GPS unavailable.
detect.address("address");
</script>

如何解决该错误?

是否有其他网站提供检测设备的功能?

我正在创建一个检测设备的页面。我使用Detect Device进行检测。但我收到一个错误:See Here

1 个答案:

答案 0 :(得分:0)

问题不是由您的代码引起的,而是由DeviceDetect类的代码中的错误引起的。它调用http://ipinfo.io/json。该代码假定一个名为&#34; hostname&#34;的属性。永远存在,但显然不是(基于你的错误)。

您是否有理由要使用此DeviceDetect类,而不是http://mobiledetect.net/变体?

如果您想继续使用DeviceDetect类,可以通过用以下代码替换246处的行来修复错误:

&#13;
&#13;
self::$ipInfo = json_decode(file_get_contents('http://ipinfo.io' . self::$ipUrl . '/json'));
self::$ipAddress = !empty( self::$ipInfo->ip ) ? self::$ipInfo->ip : '';
self::$ipInfoHostname = !empty( self::$ipInfo->hostname ) ? self::$ipInfo->hostname : '';
self::$ipInfoOrg = !empty( self::$ipInfo->org ) ? self::$ipInfo->org : '';
self::$ipInfoCountry = !empty( self::$ipInfo->country ) ? self::$ipInfo->country : '';
&#13;
&#13;
&#13;