Hi there everyone!

PHP 8.3

To start, this is not code I use to make important decisions, like serving a mobile site or other. I just use this to fill out some additional stats on visitors for my own interest.

I wrote a short block to do this but not only does it look ugly, it seems to lack things that I might want in the future, for instance, instead of just "this", if "this" OR "that" is in the string, it's a match.

I'm wondering if you guys wouldn't mind looking at this and offering any suggestions to make it better.

// Platform check
$is_win = $is_lin = $is_android = $is_iphone = $is_ipad = $is_ios = $is_mobile = $is_tablet = $is_phone = '0';
if(str_contains(strtolower($_SERVER["HTTP_USER_AGENT"]), 'windows')){
	$is_win = '1';
}elseif(str_contains(strtolower($_SERVER["HTTP_USER_AGENT"]), 'linux')){
	$is_lin = '1';
}elseif(str_contains(strtolower($_SERVER["HTTP_USER_AGENT"]), 'ios')){
	$is_android = '1';
}elseif(str_contains(strtolower($_SERVER["HTTP_USER_AGENT"]), 'android')){
	$is_ios = '1';
}

    Part of me would want to re-think having a separate variable for each. But assuming we want to go that way, you could D.R.Y. things up a bit with something like this, perhaps?

    // how about making these Booleans?
    $is_win = $is_lin = $is_android = $is_iphone = $is_ipad = $is_ios = $is_mobile = $is_tablet = $is_phone = false;
    
    // just do this in one place
    function is_type(string $type): bool {
      return str_contains(strtolower($_SERVER["HTTP_USER_AGENT"]), $type);
    }
    
    foreach([
      'is_win' => 'windows',
      'is_lin' => 'linux',
      'is_android' => 'android',
      'is_ios' => 'ios'
    ] as $key => $value) {
      $$key = is_type($value); // note the double "$$" in var name
    }
    

      Thanks a bunch for your help 🙂

      Is the double dollar sign(I looked it up and learned about a variable variable) so that it will match the value of is_win and not look for the string is_win itself?

      Regarding carrying a separate variable for each, I don't think it's necessary. I just store a value of 0 or 1 for those rows in a database since some of my rows overlap(mobile/tablet/android, for instance.) With that usage, would it still make more sense to combine the variables?

      schwim Is the double dollar sign(I looked it up and learned about a variable variable) so that it will match the value of is_win and not look for the string is_win itself?

      Yep...and I had to test it to make sure it worked, as it's not something I normally do. 😃

      You may be reinventing the wheel a bit here. I've not used it, but maybe have a look at the get_browser function. You might also consider using Google Analytics which is quite full-featured when it comes to understanding visitors to your site. The downside of google analytics is that it relies on JS to run, shares your data with the tech monster corporation, and is easily blocked.

      sneakyimp I saw that but my PHP is not configured to use browscap so I figured I'd just grab some matches in the UA string for my needs. Also, it seems that the function returns a wealth of antiquated info and I just need OS and hints towards desktop/mobile.

        Write a Reply...