Web Hosting Talk







View Full Version : Getting IP addresses w/ PHP


Joshua44
11-10-2002, 12:22 AM
Is this the right code for getting the IP address, of the user that's visiting my site? If not, what is? Thanks....


$ip = gethostbyname($SERVER_NAME);
$ip = long2ip($ip);



Oh, one other thing, foreach() isn't working.... is this the right format???

<?
$newest[1] = "value";
$newest[2] = "";
$newest[3] = "";
$newest[4] = "";
$newest[5] = "";
?>



foreach($newest as $newnum) {
fputs($file, "\$newest[$count] = \"$new\";");
$count++;
}

Ratty
11-10-2002, 01:49 AM
To get the ip address use the following statement

$ip = getenv(remote_addr);

I'm not sure what values you are trying to output to the file in the second problem you are having but try this to get your foreach working.


foreach($newest as $newvalue) {
fputs($file, $newvalue);
}

MarkIL
11-10-2002, 02:08 AM
To get the IP address, just use $_SERVER['REMOTE_ADDR'].

As for the array:


$newest = array('Value','','','',);
foreach($newest as $k=>$v)fputs($file,sprintf('$newest[$%d]=%s;',$k,$v));


Although if you're just trying to save the array into a file, you could do this:


$arr = array('a','b','c');
$fd = fopen('file','w');
fputs($fd,serialize($arr));
fflush($fd);
fclose($fd);
// ----- //
$arr = array();
$t = file('file');
$arr = unserialize($t[0]);
print join("<br />\n",$arr);

Joshua44
11-10-2002, 02:18 PM
okay, thanks, I figured out the array thing... Weird :-\