开发者

Finding out my user agent and putting it on my website

Like you can go on http://whatsmyuseragent.com/ website and see what user agent you are coming from. I need the similar feature on my website. I tried to view source and didn't find nothing. I am basically looking forward for the html code to find the user agent of people that come on my website, I am just debugging at this point. I am looking to put the coding on my home page so that I can confirm what device the user is coming from. Can a开发者_运维技巧nyone help me with this?


HTML is not a programming language. It has no means to get this information.

The proper approach is to read the User-Agent HTTP header using a server side language (and optional web framework) of your preference (e.g. in Perl/Catalyst), and then output it to the page (after sanitizing it to make it HTML safe).

Similar data is also available to client side JavaScript via the navigator object.


The user agent can be whatever the user wants it to be - so don't rely on this for security.

From the client side, you could use some JavaScript in your HTML:

<script language="javascript">
document.write(navigator.userAgent);
</script>

I don't recommend using document.write though.

For the mentioned web site, they're likely checking server side with the HTTP header. To grab this in PHP, you can use:

$ua = $_SERVER['HTTP_USER_AGENT'];

But as Quentin said, sanitise this as it could output anything the user likes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜