Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the client ip address from browser in angular (typescript)

Hey there i would really appreciate if you can provide me with an example where a type script class can get the client ip address and the browser that the client is using and to set those values in variables

i want to do this in type script not in java script is that possible and if not how to do it with type script

- So For Example i can 1) set those variables while submitting the form to the data base in the back end 2)i can for example display for the user the browser he is using any help would be appreciated thanks

like image 520
Ala'a Mezian Avatar asked Jun 14 '17 19:06

Ala'a Mezian


People also ask

How do I find my customer IP?

Determining IP Address using $_SERVER Variable Method : There is another way to get the IP Address by using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables. The variable in the $_SERVER array is created by the web server such as apache and those can be used in PHP.

How do I find my IP address in angular 8?

Firstly, import and inject the service object in the component's constructor and subscribe the getIpAddress() function from your service into a local variable, as in the following code: import { Component, OnInit } from '@angular/core'; import { IpServiceService } from './ip-service.

How do you get local IP address in AngularJS?

How Do You Get Local IP Address in AngularJS? The easiest way to get the system IP address in Angular is to send an HTTP request to a free service that returns information about addresses. One such service is AbstractAPI's Free Geolocation Endpoint.


2 Answers

Try the services of https://geolocation-db.com to get the public ip address of the user.

import { HttpClient } from "@angular/common/http";
import { catchError, tap } from "rxjs/operators";

this.http.get<any>('https://geolocation-db.com/json/')
  .pipe(
    catchError(err => {
      return throwError(err);
    }),
    tap(response => {
      console.log(response.IPv4);
    })
  )
like image 50
Kurt Van den Branden Avatar answered Oct 10 '22 14:10

Kurt Van den Branden


Thank you very much, very good solution I took it as a basis for my problem but I did not solve it because it gave me the public IP of the internet server. For an internal network with DHCP, change the URL by the following:

  getIpCliente(): Observable<string> {
      return this.http.get('http://api.ipify.org/?format=jsonp&callback=JSONP_CALLBACK') // ...using post request '
      .map((res:Response) => {console.log('res ', res);
                              console.log('res.json() ', res.text());
                              //console.log('parseado ', JSON.parse(res.text()));
                              console.log('parseado  stringify ', JSON.stringify(res.text()));
                              let ipVar = res.text();
                              let num = ipVar.indexOf(":");
                              let num2 = ipVar.indexOf("\"});");
                              ipVar = ipVar.slice(num+2,num2);
                              console.log('ipVar -- ',ipVar);
                              return ipVar}); // ...and calling .json() on the response to return data
      //.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
  }

I hope to serve you friends

like image 31
Juan Ignacio Liska Avatar answered Oct 10 '22 14:10

Juan Ignacio Liska