More reliable way of detecting if someone is mobile?

As I said in the first line of my post, it isn’t my own code. I don’t know how it works, all I know is that it DOES work