-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[bug] No user-agent has been set #946
Comments
Now that I have checked that line 1369 I can see that it's not a bug but a feature 🥇 but it's spamming the log too much, so please give instruction to have that off. Mobile-Detect/src/MobileDetect.php Lines 1368 to 1370 in 80607ae
|
@trendoman in what environment are you using this? In CLI, with Apache? I just tested this with MobileDetect 4.8.03 $detect = new MobileDetect();
// $detect->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$isMobile = $detect->isMobile();
$isTablet =- $detect->isTablet();
$deviceType = ($isMobile ? ($isTablet ? 'tablet' : 'phone') : 'computer');
$scriptVersion = $detect->getVersion();
var_dump($deviceType);
var_dump($scriptVersion); and it works by automatically looking at You could also explicitly |
I have some PHP script code running at this dev environment [link edited out] and so far very few people come there to upload and test other scripts. It makes me worry bc on production I expect many more visitors. The line which produces exceptions is very basic, pretty much boils down to this — $MB = new \Detection\MobileDetect();
var_dump( $MB->isMobile() ); I had a v.3 previously without issues, but recently tried the fresh MD 4.8.03 and now stuck with spam in error log. Any advice appreciated. Should I validate a non-empty user-agent for each visitor before creating MD object? That's possible, however I'd rather rely on MD to have that sorted out without throwing fatal errors, if that's ok with you. Edit: remove link to private server |
@trendoman
I believe the In my case:
try to set: |
You should never auto-update scripts that change major versions. In this case from |
@trendoman you can go back to using |
Thanks, this advice to get back to Just a few last notes FYI (and others that will search for this issue):
I'm probably going to resolve this (to keep using Thanks for your amazing work and attending to issues, nevertheless. |
Okay, now I fully understand the issue. I have released 4.0.04 which hopefully fixes your problem https://github.com/serbanghita/Mobile-Detect/releases/tag/4.8.04 Cheers 🙇 |
Cheers 💯 This makes redundant an unecessary complication with try-catch and is - by far - more friendly to developers. Now I can get back and throw away the ugly — $MB = new \Detection\MobileDetect();
try {
$isMobile = $MB->isMobile();
$isTablet = $MB->isTablet();
} catch ( Exception $e ) {
//https://github.com/serbanghita/Mobile-Detect/issues/946
} finally {
$isMobile = ( $isMobile == 1 ) ? 1 : 0;
$isTablet = ( $isTablet == 1 ) ? 1 : 0;
} Have a great weekend! |
Unsubscribed
|
Hi guys. Regarding web: on the one hand, that's logical that user-agent should exist when request comes. On the other hand, if User-Agent does not exist (is empty) then the backend should still return OK response in most of the cases, I guess. That's why I consider throwing exception as redundant thing. Can we just initialize with empty string? Regarding this part of code: // Setting new HTTP headers automatically resets the User-Agent.
// Set current User-Agent from known User-Agent-like HTTP header(s).
$userAgent = "";
foreach ($this->getUaHttpHeaders() as $altHeader) {
if (!empty($this->httpHeaders[$altHeader])) {
$userAgent .= $this->httpHeaders[$altHeader] . " ";
}
}
if (!empty($userAgent)) {
$this->setUserAgent($userAgent);
} Empty string is not set if we pass empty string User-Agent in header, for example: Does it make sense to change:
to:
And then remove this condition from all the methods?
The aim: avoid exceptions if the request was sent with empty |
Not wrong. I personally found that I do not need exceptions if no user agent exists. If it does not exist I make it empty myself. Here is how I decided to do this in my app — $MB = new \Detection\MobileDetect();
if( false === $MB->hasUserAgent() ){ $MB->setUserAgent(''); }
// then goes app's logic unrelevant to the topic but possibly a helpful context
if( !defined('K_USERAGENT_MOBILE') ) define( 'K_USERAGENT_MOBILE', $MB->isMobile() );
if( !defined('K_USERAGENT_TABLET') ) define( 'K_USERAGENT_TABLET', $MB->isTablet() );
unset($MB);
if( K_USERAGENT_MOBILE || K_USERAGENT_TABLET ) $k_cache_dir = K_COUCH_DIR . 'cache/mobile/'; As long as there is logic in your app about how to treat an empty ua, you are fine. The problem was with non-existent ua in the first place, which has been fixed by the @serbanghita |
@bopoda Clearly this seems to be an issue for PHP users that I didn't expect to be My current logic is:
Programmatically speaking this is okay. From a UX pov it appears to be a bad decision. I thought that the user wants a mechanism to be informed that User-Agent HTTP header is missing. I believe the solution here is that when userAgent is not set on auto-initiation phase, I will set an empty string |
@trendoman while this is fine, my aim was for users to use try/catch in PHP, I don't want you to be bothered to write extra code. I think the point here is that the library exists for a long time and it has been used in a certain way and throwing exceptions is not really something that developers need in this case |
I'll issue a patch to fix this behavior for when User-Agent is not present |
I get it. Quite anecdotally, I recently was able to find a solution to irrelevant issue in an app thanks to UA being not set e.g. caught a cURL request with a missing UA to a 404 page. This was nice to not have the UA empty in that particular case, because I was able to find the problem faster. |
yeah, this call before "auto-discovery" helps to avoid Exception.
Yeah, got it, makes sense to inform the user somehow. The case I would like to mention: If I have service and if someone sends http-requests with empty User-Agent -- FE 500 will be triggered if I don't handle it. It could trigger monitoring, spoil logs, etc. So I need to avoid 500 as this is the problem not from the server side.
Thank you! |
…ot set. By default "autoInitOfHttpHeaders" is "true", and if UserAgent is not found, it will be set to empty string "" #946 (comment)
New patch https://github.com/serbanghita/Mobile-Detect/releases/tag/4.8.05 By default it will no longer throw an Exception, it just sets the userAgent to empty string, see tests Mobile-Detect/tests/MobileDetectGeneralTest.php Lines 39 to 56 in b7a8cdd
It will throw an exception only when starting MobileDetect with Mobile-Detect/tests/MobileDetectGeneralTest.php Lines 27 to 37 in b7a8cdd
|
Following pops up in PHP error log:
The text was updated successfully, but these errors were encountered: