PDA

View Full Version : Getting a user's ip address



Rockonmetal
08-24-2007, 08:05 PM
Ok, I have a form, I want to make sure the same person isn't just making new accounts with the same IP address, so I want to make sure that I can grab there IP Address, and then keep it to see if anyone is making multiple accounts...

I already tried this script but it didn't work, because I think its a little cheaply written...


<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
// http://www.kdcgrohl.com

// Depending on your server set-up,
// you may need to use the ".shtml"
// extension [instead of the "html"
// or "htm"] as the script uses Server
// Side Includes. To display in the
// title bar, exclude the
//"<title></title>" code from the page.

// This part gets the IP
var ip = '70.16.235.110';

// This part is for an alert box
alert("Your IP address is "+ip);

// This part is for the status bar
window.defaultStatus = "Your IP address is "+ip;

// This part is for the title bar
document.write("<title>Your IP address is "+ip+"</title>");
// End -->
</script>

I'm not advertising any websites, I just want to know how I can grab the user's ip address when they fill out my form *basicly if you can get it to a

document.write("<input type='text' value='"+ipaddressfunction+"' name='userip'/ readonly='true'>
so it can come out like that, that would be great, I don't want it as an alert or in the status bar, because you never look there unless ure told to, loading something, or looking to see where ure being taken...

Twey
08-24-2007, 08:15 PM
<input type="text" value="<!--#echo var="HTTP_REMOTE_ADDR"-->" name="userip" readonly="readonly">... if you're using SSI as the original script assumed (although that one didn't use it... did you perhaps copy the source from the page rather than the source provided?

Rockonmetal
08-24-2007, 08:18 PM
I tried the source provided, but it still didn't work, and SSI? whats that? never heard of it... oh and it didn't work... I don't know why... i tried with it in document.write and without, it didn't work for both...

If there is a PHP way of doing this thats also ok...

thetestingsite
08-24-2007, 08:48 PM
You can do it one of two ways with PHP. Either the route that you were going to take:



<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
// http://www.kdcgrohl.com

// Depending on your server set-up,
// you may need to use the ".shtml"
// extension [instead of the "html"
// or "htm"] as the script uses Server
// Side Includes. To display in the
// title bar, exclude the
//"<title></title>" code from the page.

// This part gets the IP
var ip = '<?php echo $_SERVER['REMOTE_ADDR'];?>';

// This part is for an alert box
alert("Your IP address is "+ip);

// This part is for the status bar
window.defaultStatus = "Your IP address is "+ip;

// This part is for the title bar
document.write("<title>Your IP address is "+ip+"</title>");
// End -->
</script>


or like so (Just place where you want to display it:



<?php echo $_SERVER['REMOTE_ADDR'];?>


(Note: This has to be in a php enabled page so that it parses correctly.)
Hope this helps.

Rockonmetal
08-24-2007, 08:55 PM
Unfortunitly I can't view PHP pages at the moment *server is holding it self hostage, i'll be fine* , but could someone check to see if this works?

<?php echo "<input type='text' value='$_SERVER['REMOTE_ADDR']' name='useripaddress' readonly='true' />?>
thanks for testing it! *You don't need to send it, if it displays it will most likely send!* thanks!

thetestingsite
08-24-2007, 09:10 PM
The following WILL work:



<?php echo "<input type='text' value=' " . $_SERVER['REMOTE_ADDR'] . " ' name='useripaddress' readonly='true' />" ?>


Hope this helps.

Rockonmetal
08-24-2007, 09:15 PM
k thanks a lot man

Twey
08-24-2007, 09:25 PM
I tried the source provided, but it still didn't workDid you try it with a .shtml extension as the script said?
<?php echo "<input type='text' value='$_SERVER['REMOTE_ADDR']' name='useripaddress' readonly='true' />?>It won't work because that's a complex variable: you need braces around it. However, there are deeper problems: "true" is not a valid value for the readonly attribute (can only be "" or "readonly"), you shouldn't be using XHTML (at least unless you're prepared to serve it as such; since you used document.write() earlier I doubt you're doing so) and echoing strings of HTML is bad form (don't output HTML from PHP parsing mode, since it adds an unnecessary layer of parsing and detracts from readability and efficiency).
<input type="text" name="user_ip" readonly="readonly" value="<?php echo $_SERVER['HTTP_REMOTE_ADDR']; ?>">is the PHP way of doing it, or
<input type="text" name="user_ip" readonly="readonly" value="<?=$_SERVER['HTTP_REMOTE_ADDR']?>">with short tags.

Rockonmetal
08-24-2007, 09:34 PM
I did try with the .shtml extension but as long as Testingsite's version works, I'll be fine...


It won't work because that's a complex variable: you need braces around it. However, there are deeper problems: "true" is not a valid value for the readonly attribute (can only be "" or "readonly"), you shouldn't be using XHTML (at least unless you're prepared to serve it as such; since you used document.write() earlier I doubt you're doing so) and echoing strings of HTML is bad form (don't output HTML from PHP parsing mode, since it adds an unnecessary layer of parsing and detracts from readability and efficiency).

hun? So are you saying its a bad idea to do this?

<?php
echo "form data stuff *inputs and labels"
?>
because I'm not doing that i'm doing this:


<script>
document.write("formdataandcodes")
</script>
which prevents the user from have javascript disabled so he can pass the validation without valid content

joshlindem
10-15-2007, 09:06 PM
ok, so I read this thread and it was great. Now I am able to secretly grab the IP address of the person filling out the web form and place it within the email I'm sending to myself.
(By the way, this is done within a PHP script that runs as soon as you hit Submit.. so it's not seen as part of the form)

But here's what I found. I tested this out at work today and it did indeed give me my IP address. But this is my IP address within the company. Is there any way to additionally add the outside IP address as well? (ie: the IP address of the outside firewall or port that is used to connect them to their ISP?)

If I needed to track down a person... the internal definitely helps, but I wouldn't be able to locate what company/location it came from.

Thanks