Jeevs
Jeevs

Reputation: 969

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 anyone help me with this?

Upvotes: 3

Views: 2019

Answers (2)

Karl Andrew
Karl Andrew

Reputation: 1555

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.

Upvotes: 2

Quentin
Quentin

Reputation: 943579

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.

Upvotes: 4

Related Questions