Warning: gethostbyaddr(): Address is not a valid IPv4 or IPv

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Warning: gethostbyaddr(): Address is not a valid IPv4 or IPv

Post by fresh »

Edit:

heres my fixed code:

Code: Select all

<?
require 'db_connect.php';

if (strstr($_SERVER['REMOTE_ADDR'], ', ')) {
   $ips = explode(', ', $_SERVER['REMOTE_ADDR']);
   $uvisitor = $ips[0];
} else {
    $uvisitor = $_SERVER['REMOTE_ADDR'];
}

$exptime=$utime-600; // (in seconds)



@mysql_query("delete from online where timevisit<$exptime");

$uexists=@mysql_num_rows(@mysql_query("select id from online where visitor='$uvisitor'"));



if ($uexists>0){

@mysql_query("update online set timevisit='$utime' where visitor='$uvisitor'");

} else {

@mysql_query("insert into online (visitor,timevisit) values ('$uvisitor','$utime')");

}

$rs=@mysql_query("select * from online");

echo "<style><!--\n";

echo "body {font-family:verdana;font-size:10pt}\n";

echo "td {font-family:verdana;font-size:10pt}\n";

echo "--></style>\n";

echo "<div align=center><table><tr bgcolor=#CCCCCC><td><b>Visitor 
IP/Host<td><b>Last visit</tr>";

while ($ro=@mysql_fetch_array($rs)){

echo "<tr><td>".$ro[visitor]."<td>".date('j M Y - H:i',$ro[timevisit])."</tr>";

}

echo "</table></div>";

$jmlonline=@mysql_num_rows(@mysql_query("select id from online"));

echo "<div align=center><b>There are $jmlonline user online</b></div>";

?>
this is the whosonline.php script. Why is it giving me this error? How can I fix it? Also, how would I get the id's into the table, would I need to include some code in my login page? If so, what would I need to write?

thanks
Last edited by fresh on Sun Jul 04, 2004 11:54 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try instead:

Code: Select all

$uvisitor=$_SERVER['REMOTE_ADDR'];
Also, sometimes more than one IP address is returned, eg. '145.222.131.261, 196.149.25.113' so you might also consider:

Code: Select all

if (strstr($_SERVER['REMOTE_ADDR'], ', ')) {
   $ips = explode(', ', $_SERVER['REMOTE_ADDR']);
   $uvisitor = $ips[0];
} else {
    $uvisitor = $_SERVER['REMOTE_ADDR'];
}
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

hey

Post by fresh »

thanks alot, that script worked and took care of the warning...

one question though... how would I get the IP's into my DB table 'online'...?

Do I need to put some script in with my login page, which issuse the sid and such, but does not currently add anything to the online table... I don't have any idea how to go about adding to the online table, or where to do so, I logged in and when I goto the whosonline page, it says 0 users online, so I check the db and no entries go to the table... so I suppose I need to do so within the login page, however, I have no idea how to do so... any help?
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

In your login page, just execute a MySQL query when the user logs in. For example:

Code: Select all

//...
$instert_online_qury = "INSERT INTO online username, ip VALUES $username, $uvisitor";
mysql_query($instert_online_query, $db_connection);
//etc, etc...
Be sure to remove that row when the user logs out or closes his browser.
Post Reply