Jump to content

[SOLVED] How To Capture IP Address?


inferium

Recommended Posts

Hello, PHP n00blet here (though I'm starting to understand the language a bit better as of late). My boss wants me to be able to capture our clients' IP address in our online application here: http://apply.eliteautoweb.com. The form is made in html, as I don't know how to do PHP forms yet.

 

So far in my searching, I've found the following snippet of code:

 

<?

$logged_string = "$REMOTE_ADDR|" . date("j M Y g:i a");

$file = fopen("userIP.log", "a");

fputs($file, $logged_string, strlen($logged_string));

fclose($file);

?>

 

 

Is there a way that I can capture an IP address within the form using this code, or is there another more efficient way to capture IP? Thanks :D

Link to comment
https://forums.phpfreaks.com/topic/137225-solved-how-to-capture-ip-address/
Share on other sites

<?php
$logged_string = $_SERVER['REMOTE_ADDR'] . "|" . date("j M Y g:i a");
$file = fopen("userIP.log", "a");
fputs($file, $logged_string, strlen($logged_string));
fclose($file);
?>

 

That is the proper usage, $REMOTE_ADDR assumes register_globals is on, which is should be off due to security risks. Use $_SERVER to access it instead.

 

As far as that, it should capture and IP just fine and write it to a log file, or if you have a database make a field called IP and save it to the database.

Sweet deal :) So I'm guessing there is no way to have the address sent as a part of the application? Having a unique IP for each of our clients is the main goal I am trying to accomplish.

 

I think that's a very vague question. What do you mean "sent" as part of the "application?" You can put the IP address in a hidden input field.

 

<input type="hidden" name="ip_address" value="<?=$_SERVER['REMOTE_ADDR']?>" />

 

and handle that through your post/get vars, or, you can just get the IP as we all have already explained.

Archived

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

×
×
  • Create New...

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.