Getting IP address through AJAX?

Say I submit a form to a php script through AJAX on my website. In the php script (which receives the form submission), if I try to retreive the IP address of the user who submitted the form (through $_SERVER[‘REMOTE_ADDR’]), will I get the correct IP of the user? Or will I have to find the IP address and submit it through javascript?

$_SERVER[‘REMOTE_ADDR’] would work.

It has to be done that way because the only way that Javascript can get the IP address is if server side code passes it to the Javascript.

May I ask if you let me know in detail :mad: of how and where I could put a code which takes the IP address of the other users of a web-site. And then what is the simplest form of that code.
Bests, Keivan.

Absolute bare bones way:

  1. On a web page somewhere:

<script src="get_ip.php" language="javascript" type="text/javascript"></script>

  1. Now the “get_ip.php” file:

echo 'document.write("' . $_SERVER['REMOTE_ADDR'] . '");';

The script tag will look to the get_ip.php file for javascript code. Because it is a PHP file, it will be parsed by the server and return Javascript to write out the user’s IP address.

I put a button on my web page as follows:
<a href=“javascript:get_ip();”><img src=“images/get.gif” border=“0”></a>

<script language=“javascript” type=“text/javascript”>
function get_ip(){ src=“get_ip.php”;

where “get_ip.php” is a simple separate page [both in my server] as following:
echo ‘document.write("’ . $_SERVER[‘REMOTE_ADDR’] . ‘");’;

What should I do then to see the result? My next question is that how may I get and see the ip of anyone who is using of my web page.

Many thanks to you, Keivan

Instead of this:

<script language="javascript" type="text/javascript">
function get_ip(){ src="get_ip.php";

do this:

<script language="javascript" type="text/javascript" src="get_ip.php"></script>

Put the script tag above your button somewhere, like in the <head> section.

Then, you could do this:

<a href="javascript:get_ip();"><img src="images/get.gif" border="0"></a>
<td id="ip_address">

And, then change your get_ip.php file to this:

echo ‘function get_ip(){’;
echo ‘document.getElementById(“ip_address”).innerHTML = "’ . $_SERVER[‘REMOTE_ADDR’] . ‘"’;
echo ‘}’;

Remember, the output of the PHP file needs to be good Javascript. What I usually do is write the Javascript first and then add the echo statements afterwards.

Dear darkwater23,
I actually did all you said. I receive the message “javascript error: object expected”.

To tell you the truth, I did not get exactly what you meant by
“Remember, the output of the PHP file needs to be good Javascript. What I usually do is write the Javascript first and then add the echo statements afterwards.”

I also put the code at get_ip.php in a <script>,</script> tags, again that error.

My origin question is that without clicking on a buttom or sth else, what shoud I do to take and show the ip of anyone who is visiting my web-page. In tags like “Your Ip address is:”+IP_USER+".

Thanks for a quick reply.

To clarify, PHP is usually used for dynamic HTML. You can make database calls, lookup server variables or write to files and display the results in HTML. But, you can use PHP to make almost anything dynamic. In this case, we are making dynamic Javascript.

So, any echo or print statement in PHP needs to “write out” normal Javascript. What makes this hard is usually quotes. You have to make sure that a printed line in PHP contains the correct number of quotes.

As for the error you are receiving, you must be doing something different from me. There should be no script tags in the PHP file (except for the <?php ?> tags).

The object error could mean that you didn’t put an id attribute on the cell you wanted. The id field is key in my example. Without it, it won’t work.

If you are still having problems, you need to post your HTML code.

Good luck!

[B][COLOR=DarkRed]Say, one might be a help to me is the last part of my question. How may I take the IP address of your system who are visiting my web-page.

I need to regist this IP in a database for further investigation. Please forget that idea of button and click on them. What code enables me to take the IP address of the others who logged into my server and using the facilities.