Get Users Local LAN IP address using php, JavaScript, ASP.net
How to GET Local LAN IP Address of user using PHP, Javascript, ASP.net. I have also written a blog how to get real public IP of user, but my current Application is intranet based so public IP for all clients is same that is 127.0.0.1
So that is not enough to trace different activities by public IP, So we have to get Local IP. luckly WebRTC extension of HTML5 give us access to get User’s local IP address using javascript.
But how can we get this Local IP at server side at (PHP, ASP.net etc)
It is easy, just create hidden input in form and using JavaScript on pageload get IP and feed into that hidden input. that will be submitted with form to PHP or ASP.net
Step 1: Hidden Input
Create hidden input in form with id=”localIP” and name=”localIP” as shown in below form
1 2 3 4 5 6 7 |
<form method="POST"> <!--Other form elements --> <input type="hidden" name="localIP" id="LocalIP" /> <!--Other form elements --> </form> |
Step 2: JavaScript Code
This is actual code which gives us the Local LAN IP Address of users, and feed it in above hidden input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<script> try{ // NOTE: window.RTCPeerConnection is "not a constructor" in FF22/23 var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection; if (RTCPeerConnection) (function () { var rtc = new RTCPeerConnection({iceServers:[]}); if (1 || window.mozRTCPeerConnection) { // FF [and now Chrome!] needs a channel/stream to proceed rtc.createDataChannel('', {reliable:false}); }; rtc.onicecandidate = function (evt) { // convert the candidate to SDP so we can run it through our general parser // see https://twitter.com/lancestout/status/525796175425720320 for details if (evt.candidate) grepSDP("a="+evt.candidate.candidate); }; rtc.createOffer(function (offerDesc) { grepSDP(offerDesc.sdp); rtc.setLocalDescription(offerDesc); }, function (e) { console.warn("offer failed", e); }); var addrs = Object.create(null); addrs["0.0.0.0"] = false; function updateDisplay(newAddr) { if (newAddr in addrs) return; else addrs[newAddr] = true; var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; }); document.getElementById('localIP').value = displayAddrs.join(" or perhaps ") || "n/a"; } function grepSDP(sdp) { var hosts = []; sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39 if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13 var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1 addr = parts[4], type = parts[7]; if (type === 'host') updateDisplay(addr); } else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7 var parts = line.split(' '), addr = parts[2]; updateDisplay(addr); } }); } })(); else { document.getElementById('localIP').value = ""; } }catch(err){ //if any error occured or not supported, reset value of hidden input document.getElementById('localIP').value = ""; } </script> |
Now as soon someone open this page, hidden input will get Local LAN IP Address of that user. on form submission it will be sent to server side (PHP/ASP.net)
Step 3: Process at Server-side
here i will just show getting this IP only using PHP, ASP.net and other languages will also have same easy process
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php if($_POST){ //get other submitted inputs $localIP = $_POST['LocalIP']; echo $localIP; exit; //after checking you can save it in Database or what ever you want } ?> |
This is all done, i think it was easy and I hope you will not have any issue to implement this in your own project(s).
at anytime if you need help you can write comment, i will try my best to help you out.
Comments