最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - User IP and useragent - Stack Overflow

programmeradmin5浏览0评论

I'm building a web app via a 3rd party API and they are requesting that I pass the user IP and user agent for each call.

I prefer javascript for this (I am using Angular) or php as I'm using curl for the request. I can find a 3rd party script for this but only for the user IP and not useragent. I would prefer one that does both to cut down the number of calls.

Can anyone help with a script or website that handles this?

I'm building a web app via a 3rd party API and they are requesting that I pass the user IP and user agent for each call.

I prefer javascript for this (I am using Angular) or php as I'm using curl for the request. I can find a 3rd party script for this but only for the user IP and not useragent. I would prefer one that does both to cut down the number of calls.

Can anyone help with a script or website that handles this?

Share Improve this question asked Jan 24, 2016 at 3:56 ottz0ottz0 2,6057 gold badges30 silver badges51 bronze badges 3
  • sure ...it's called google ...and where you should have looked in the first place. Questions asking for external resources are off-topic here – charlietfl Commented Jan 24, 2016 at 4:14
  • Mate...i've searched google and have e back around in a big circle and found nothing which is why i'm asking the question here in the attempt someone may point me in the right direction – ottz0 Commented Jan 24, 2016 at 4:24
  • can get it right from $_SERVER in php.. do a dump – charlietfl Commented Jan 24, 2016 at 4:28
Add a ment  | 

2 Answers 2

Reset to default 4

In PHP:

<?php
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    $ip = $_SERVER['REMOTE_ADDR'];
?>

I don't really know PHP, this was just the result of Googling your question. Please try to do some research first!

In JS:

var userAgent = window.navigator.userAgent;
var req = new XMLHttpRequest();
req.onload = function() {
  var ip = JSON.parse(req.response).ip;
  //Use ip asynchronously here
};
req.open("GET", "https://api.ipify/?format=json");
req.send();

Getting the IP is not possible in JS unless you abuse a security vulnerability in WebRTC or query a third-party service, as demonstrated here. It's important to know that ip doesn't exist outside of req.onload, and therefore cannot be used until the request has responded.

If you have access to ES7 (unlikely but I'll include it for pleteness), you can use this to get a user's IP:

(async () => {
  var ip = (await fetch("https://api.ipify/?format=json").then(r => r.json())).ip;
  //use ip
})();

Which is shorter and more interesting.


As a side note, there shouldn't be much need to ever get the user's IP on the client side, since any request you make in JS will e from that client. This means that whatever server is receiving that request will be able to see the IP natively, without hacks like this.

I'm not sure why the third party API requests you pass the client IP if the client is making the request - sounds like a bad API.

What does all of that mean?! Each bit of your user agent indicates something particular about your system.

There's no "standard" way of writing a user agent string, so different web browsers use different formats (some are wildly different), and many web browsers cram loads of information into their user agents. That's where WhatIsMyBrowser. steps in - we decode your user agent string to figure out everything it's saying.

发布评论

评论列表(0)

  1. 暂无评论