-1

I need to get the computer IP address and I need to check if that IP address is in my database. If it was, I need to display the name registered to that IP address that was in my database.

Actually I am confused because there were no error detected yet there were no output displayed.

Here's my code


<?php
$ipname = gethostbyname(trim(`hostname`));
$ip_query = "SELECT ip_address FROM table 5";
$ip_add = mysql_query($ip_query);

if ($ip_add === $ipname){
echo "{$ip_add['guest_name']}";
}
?>
wesley
  • 1
  • 1
  • 2
    Welcome to Stack Overflow! Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Blue Aug 04 '16 at 07:25
  • you can use the function mentioned in this answer http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php – Diab Aug 04 '16 at 07:27
  • @Diab What's that have to do with his question? – Blue Aug 04 '16 at 07:30
  • @FrankerZ I'm sorry.I'm new at this, but I hope that you can help me. When I try the first line only and echo $ipname; it displays my IP Address. but when I add the rest of the code it displays nothing and there were no error detected – wesley Aug 04 '16 at 07:50
  • @wesley In order for us to help us, help you, you need to learn some basic debugging techniques. See [this post](http://blog.teamtreehouse.com/how-to-debug-in-php) and [mysql_error](http://php.net/manual/en/function.mysql-error.php) to fetch the error if a query failed. This code is literally riddled with errors. – Blue Aug 04 '16 at 07:54
  • Please take a look at the documentation for mysql_query, in particular the return types as well as how to check error codes, as others have suggested. In addition, it's not clear to me that you have ever connected to the MySQL database. I recommend that you update the example code with a complete standalone example. – jwriteclub Aug 04 '16 at 08:38
  • @wesley: Atleast respond back to the answer given below. – Nana Partykar Aug 04 '16 at 08:49

2 Answers2

0

the way you are retrieving the IP won't work in some cases, so first you need to get it using the function implemented here. for the query itself the query is retrieving the first 5 rows only, so you need to add "where" part so the query should be something like that $ip_query = "SELECT ip_address FROM table where ip = $ipname"; then after that you shouldn't compare the output of mysql_query, the output is a mysql resource you should use another function to get the result

$num_rows = mysql_num_rows($ip_add) 
if ($num_rows>=1) { 
$ip = mysql_result($ip_add,0,'ip_address'); 

// then compare the ip variable with the one you have from the function 

}
Community
  • 1
  • 1
Diab
  • 142
  • 7
0

Find, IP Address through given function. Check in query there itself. If ip address exist, Print guest name.

[NOTE: I Used YourTableName as table name. Put appropriate table name in query.]

<?php
$ipname = getIpAddress();

$query = mysql_query("SELECT guest_name FROM `YourTableName` WHERE ip_address = '$ipname'");
$countUser = mysql_num_rows($query);

if($countUser > 0){
  while($row = mysql_fetch_array($query)){
    echo "User Name: ".$row['guest_name']."<br>";
  }
} else {
  //Your Logic
}

function getIpAddress() {
  if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    return $_SERVER['HTTP_CLIENT_IP'];
  } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    return trim($ips[count($ips) - 1]);
  } else {
    return $_SERVER['REMOTE_ADDR'];
  }
}
?>

NOTE: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77