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
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.
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? 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.
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);
})
)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With