I have described a way to do this in my answer to stackoverflow.com/questions/9297370/. The strategy is:
Use css media queries to detect phones using a max-device-width of 600px.
Set an imperceptible change in style on this basis, e.g. body color from rgb(0, 0, 0) to rgb(0, 0, 1)
Use an OnLoad() to call a Javascript function that checks the body color.
(4. Override the style change if necessary in your style sheet for classes, ids or tags etc.)
This depends on 600px being the correct device-width discriminator. At present it works with all iPhones (up to 6plus) v. all iPads (using Apple's Xcode simulator). Can't guarantee that there aren't bigger phones, but if they are they would probably tolerate being treated as tablets.