admin管理员组

文章数量:1430827

I am working on an application, and there is a page that I only want it to be available for users accessing from iphone devices..

As we know, user can change the user-agent from the client side or through the browser emulator, so I wanna know if there is a way to prevent it please?

Note: I managed to prevent that after user login to my website by keeping the browser user-agent in the session at the time of login.. so even if the user changes the user-agent after that, I will only consider the previous user-agent I saved in the session at the time of the login

However, what might not work is what if the user changes the user-agent to 'iphone' using emulator before calling my website and creating the session..

Can u help me with that please? Thanks,

I am working on an application, and there is a page that I only want it to be available for users accessing from iphone devices..

As we know, user can change the user-agent from the client side or through the browser emulator, so I wanna know if there is a way to prevent it please?

Note: I managed to prevent that after user login to my website by keeping the browser user-agent in the session at the time of login.. so even if the user changes the user-agent after that, I will only consider the previous user-agent I saved in the session at the time of the login

However, what might not work is what if the user changes the user-agent to 'iphone' using emulator before calling my website and creating the session..

Can u help me with that please? Thanks,

Share Improve this question edited Dec 24, 2015 at 16:05 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Dec 24, 2015 at 16:00 J-CoderJ-Coder 615 bronze badges 2
  • 6 You can't prevent it. – sstan Commented Dec 24, 2015 at 16:02
  • 3 You'll have about much chance of success with this as I would have ordering you to not breathe anymore... – Marc B Commented Dec 24, 2015 at 16:03
Add a ment  | 

6 Answers 6

Reset to default 3

There is no reliable way to verify a client's User-Agent. At best, you might be able to make inferences using other information about their browser, e.g. if they say they are an iPhone, but they have a 1920x1080 screen/browser window, something isnt right. But I would not remend this.

Simple answer - you can't.

However, you can detect that the user is browsing on a mobile device using CSS media queries which you could use to show/hide a warning saying that the page is only available to mobile devices.

This still won't stop people spoofing the user agent though, and there is nothing you can do about that.

A trick that could work, is to check navigator.platform. See this post

But if someone really wants to see this page without an iPhone, Javascript is not secure.

Note, untested at iPhone, though returns expected results filtering Webkit, Moz , Ms prefixes in browsers Use Jquery to detect IE, then redirect , How to target only one browser?

Try checking for -apple prefix in document.body.style

if ("AppleUserSelect" in document.body.style) {
  // do stuff
}

See Vendor-prefixed CSS Property Overview

You could detect which browser is used by detecting browser specific features, like some proprietary Javascript functions.

Also, search for "browser fingerprint" on Google, it might help you.

Thank you all for the answer. To be in the save side, I'll put in my mind that if someone intends to access the page, can sniff and change the user-agent..

本文标签: javascriptHow to prevent useragent to be changed by userStack Overflow