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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.