|
Posted by J.O. Aho on 09/08/07 16:04
Rob wrote:
> Just wondering how sites capture a visitor's IP address and how this is
> done in PHP?
There is a page which lists PHP global variables, these are useful to know,
http://www.php.net/manual/en/reserved.variables.php
As you see $_SERVER['REMOTE_ADDR'] will hold the remote users IP-address (if
the remote user is using a proxy, then it's the proxy IP-address you get).
So in your code you can use:
<?PHP
$userip=$_SERVER['REMOTE_ADDR'];
echo "This is your ip-number: {$userip}<br>";
//File we want to store the ip-number to
//se to have this file already.
$filename = 'storeipnumbers.txt';
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $userip) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
echo "We have stored ip-number '{$userip}' to file '{$filename}'";
} else {
echo "Sorry, we couldn't store the following ip-number: {$userip}<br>";
}
?>
This script displays the ip-number and stores it to a file called
storeipnumbers.txt.
--
//Aho
[Back to original message]
|