Socket
Socket
Sign inDemoInstall

parse-user-agent

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    parse-user-agent

parseUserAgent is a function that takes a user agent and returns the browser name and version (and soon the operating system and version).


Version published
Maintainers
1
Created

Readme

Source

Description

parseUserAgent is a function that takes a user agent and returns the browser name and version (and soon the operating system and version).

The Wheel

There are many libraries and solutions for this problem, but I decided to re-invent the wheel and write my own as I've not found one that did all of the following:

  1. Pattern-based, I don't want to store huge lists of known-good agents
  2. Local, I don't want to send requests to (or pay for) an external API
  3. Reliable, I've tested lots of libraries, many have incorrect results
  4. Specific, I don't just want the main 6~ and then "Other"

Todo

  1. Operating System & Version parsing
  2. Explore cache (normal, session, or temp-file) solutions

PHP

Usage

<?php

$user_agent = $_SERVER['HTTP_USER_AGENT'];
// Mozilla/5.0 (X11; Linux x86_64; rv:2.0b9pre) Gecko/20110111 Firefox/4.0b9pre

print_r(
    parseUserAgent($user_agent)
);

The above code will output:

Array
(
    [browser_name] => Firefox
    [browser_version] => 4.0b9pre
)

JavaScript

let user_agent = request.headers["user-agent"];
// Mozilla/5.0 (X11; Linux x86_64; rv:2.0b9pre) Gecko/20110111 Firefox/4.0b9pre

console.table(
    parseUserAgent(user_agent)
);

The above code will output:

┌─────────────────┬────────────┐
│     (index)     │   Values   │
├─────────────────┼────────────┤
│  browser_name   │ 'Firefox'  │
│ browser_version │ '4.0b9pre' │
└─────────────────┴────────────┘

Keywords

FAQs

Last updated on 02 Oct 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc