serbanghita / Mobile-Detect

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
http://mobiledetect.net
MIT License
10.52k stars 2.67k forks source link

I dont understand #951

Closed ioempire closed 7 months ago

ioempire commented 7 months ago

Im try to use the Mobile-Detect 4.8.x Screenshot_20240116_094918_Total Commander This is my server path

My php code look like this: [$mobil=0;

require($_SERVER['DOCUMENT_ROOT']."/Mobile-Detect/src/Mobile_Detect.php");

use Detection\MobileDetect;

$detect = new MobileDetect();

$detect->setUserAgent();

if( $detect->isMobile() && !$detect->isTablet() ) $mobil =1;](url)

I get this error: [16-Jan-2024 08:54:11 UTC] PHP Fatal error: require(): Failed opening required '/home/friudeni/public_html/Mobile-Detect/src/Mobile_Detect.php' (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/friudeni/public_html/index.php on line 28

What is wrong?

microchip8 commented 7 months ago

it's MobileDetect.php

Not Mobile_Detect.php

serbanghita commented 7 months ago

@ioempire you downloaded 4.x version which contains the file /src/MobileDetect.php

thank you @microchip8

ioempire commented 7 months ago

Thanks Now I get this error:

[16-Jan-2024 09:49:03 UTC] PHP Fatal error: Uncaught Error: Class "Detection\Cache\Cache" not found in /home/friudeni/public_html/Mobile-Detect/src/MobileDetect.php:1032 Stack trace:

0 /home/friudeni/public_html/index.php(30): Detection\MobileDetect->__construct()

1 {main}

thrown in /home/friudeni/public_html/Mobile-Detect/src/MobileDetect.php on line 1032

In line 30 I have: $detect = new MobileDetect();

I Use php version 8.0

serbanghita commented 7 months ago

@ioempire it's because you're not using composer autoload like in this example https://github.com/serbanghita/Mobile-Detect/blob/4.8.x/scripts/example.php

you will have to manually include all dependent files from https://github.com/serbanghita/Mobile-Detect/tree/4.8.x/src

ioempire commented 7 months ago

But what is this line: require_once DIR . '/../vendor/autoload.php'; vendor and autoload.php is not in the Mobile-Detect directory

ioempire commented 7 months ago

Now my code look like this: $mobil=0;

include($_SERVER['DOCUMENT_ROOT']."/Mobile-Detect/src/Cache/CacheException.php"); include($_SERVER['DOCUMENT_ROOT']."/Mobile-Detect/src/Cache/Cache.php"); include($_SERVER['DOCUMENT_ROOT']."/Mobile-Detect/src/Cache/CacheItem.php"); include($_SERVER['DOCUMENT_ROOT']."/Mobile-Detect/src/Exception/MobileDetectException.php"); require($_SERVER['DOCUMENT_ROOT']."/Mobile-Detect/src/MobileDetect.php");

use Detection\MobileDetect; $detect = new MobileDetect(); $detect->setUserAgent(); if( $detect->isMobile() && !$detect->isTablet() ) $mobil =1;

And I get this error: [16-Jan-2024 10:56:06 UTC] PHP Fatal error: Uncaught Error: Interface "Psr\SimpleCache\CacheInterface" not found in /home/friudeni/public_html/Mobile-Detect/src/Cache/Cache.php:7 Stack trace:

0 /home/friudeni/public_html/index.php(30): include()

1 {main}

thrown in /home/friudeni/public_html/Mobile-Detect/src/Cache/Cache.php on line 7

serbanghita commented 7 months ago

@ioempire 4.8 is meant to be used with composer with composer require mobiledetect/mobiledetectlib, in the above case you're missing a dependency.

I'm sorry for misguiding you, I didn't realised that 4.8 is fully dependent on composer autoload (which is kinda like what modern PHP env looks like).

Just start from scratch use the code from 2.8 branch https://github.com/serbanghita/Mobile-Detect/tree/2.8.x then you don't need to include anything, see https://github.com/serbanghita/Mobile-Detect/blob/2.8.x/examples/test.php - it's the same detection, but the code is compatible with classic way of manually including the class.

serbanghita commented 7 months ago

In summary:

4.8 - use composer and example https://github.com/serbanghita/Mobile-Detect/blob/4.8.x/scripts/example.php 2.8.x - you can use classic include - https://github.com/serbanghita/Mobile-Detect/blob/2.8.x/examples/test.php