-8

Possible Duplicate:
Get Client IP using just Javascript?

i am developing a online chat application, for that i have a server with static ip. i want to give only small javascript to include in client website. here my requirement is obtain ip address and the location of the user of the website. websites my in in any language like php,jsp,html,asp....for any wesite my script is same please help me in this issue to solve.

Community
  • 1
  • 1
G V S Vinayak
  • 51
  • 1
  • 6

2 Answers2

0

You cannot retrieve user IP with just Javascript . Javascript is a client side programming language, so you should use AJAX and connect to server to get the IP address with a server side programming language.

UPDATE:

You can get Dynamic and Static ip with this code in PHP:

$domain=$_SERVER['REMOTE_ADDR'];
$domain1 = isset($_SERVER['HTTP_X_FORWARDED_FOR']);
echo "Viewer's Server's IP Address: <strong>$domain</strong> - ";
echo "Viewer's PC IP Address: <strong>$domain1</strong>";

If you want to return with json you can use this :

$domain=$_SERVER['REMOTE_ADDR'];
$domain1 = isset($_SERVER['HTTP_X_FORWARDED_FOR']);
$data = array('static' => $domain1,
              'dynamic' => $domain);
echo json_encode($data);
MajAfy
  • 3,007
  • 10
  • 47
  • 83
0

No need to use AJAX.
In your PHP:

$_SERVER['REMOTE_ADDR']

That's the connected client's IP, assuming they're not using a Proxy and whatnot.
Add this IP to a variable in your script if you need to use it in your JS.

Community
  • 1
  • 1
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • if my site is in html how can i retrive ip address? – G V S Vinayak Jan 29 '13 at 11:06
  • Then you're going to have to make a AJAX request to your server, where the server returns the IP. The point is, that AJAX call requires a server-side file (php) to return the IP, you might as well just add the IP through php immediately. So, what I'm trying to say is: There is absolutely **no point** in making a AJAX request to get the IP. You're going to have to use a server-side language one way or the other, so you might as well limit the amount of requests while you're at it. – Cerbrus Jan 29 '13 at 11:09
  • i am very thank full to you for giving responses to my questions..i will explain one scenario.i have one site www.clientsite.com and another site that is chat server name like www.chatserver.com, client site is in pure html.and server is side is php.how can i synchonize these two and tell me the process of getting ip of clientsite visitor – G V S Vinayak Jan 29 '13 at 11:16
  • please tell me the process of doing then i will try – G V S Vinayak Jan 29 '13 at 11:24
  • Like I said, just get the IP from php's `$_SERVER['REMOTE_ADDR']` – Cerbrus Jan 29 '13 at 11:26