I don't see what the point of this is:
$query = "SELECT * FROM ip WHERE ip = '$ip'";
/* query the database */
$result = mysql_query($query);
if (mysql_fetch_row($result))
{
$search= "SELECT User FROM users where ip = '$ip'";
You do a query, and if it succeeds, you do another query that asks for less information. The second query literally tells you nothing you don't already know.
$query = "SELECT user FROM ip WHERE ip = '$ip'";
/* query the database */
$result = mysql_query($query);
if ($row=mysql_fetch_array($result))
{
list($user) = $row;
print $user;
}
else
{
header('Location: --');
}