Jump to content

Archived

This topic is now archived and is closed to further replies.

jaffy

Capturing IP and placing in text field

Recommended Posts

Hi,
I am a complete newbie and have a ready made PHP script installed. However one thing it is missing is a way to capture the visitors IP address and storing it in the database when they register. I have found out how to get the visitors IP address with this;

[code]<?php
$ip = $_SERVER['REMOTE_ADDR'];

echo "Your IP address is: $ip"; [/code]

I have a redundant field on the register page which is used to enter a website, so I wish to simply use this field to automatically enter the visitors IP adress

So my question is, how do I get the $ip variable into the value field of an input field like this one;

[code]<input type="text" name="IP Adress" value="{m_website}">[/code]

Thanks for any help

Share this post


Link to post
Share on other sites
[code]
<input type="hidden" name="IP_Address" value="<?php echo $_SERVER['REMOTE_ADDR'];?>" />
[/code]

this is a hidden field, and is not being displayed to the user (unless they do view - source) so when he submit the form you will be able to "capture" his ip by using:

[code]
<?php
# get the actual ip address by using the associative key created by the form (IP_Address)
$ip = $_REQUEST['IP_Address'];

# display ip
echo $ip;
?>
[/code]

Share this post


Link to post
Share on other sites
The above method could be more easily 'spoofed' or neglected entirely.  If you want to add it into the database, make sure of the database structure (ie: make sure you have a field for the IP address) and do something similar to this...

[code]
<?php
$IP = $_SERVER['REMOTE_ADDR'];
$Query = mysql_query("INSERT INTO `table` (`field`) VALUES ('$IP')");
?>
[/code]

Obviously this will need to incorperate your current structure and be added to your registration database query that you already have.

HTH

Dest

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.