1

So, I have a couple of variables in PHP, and they are generated from different places. $new_ip comes from the following command:

$new_ip = file_get_contents('http://www.ipaddresscheck.comlu.com/ip.php');

And the $old_ip comes from:

$sql = mysql_query('SELECT * FROM ip ORDER BY  id DESC LIMIT 1');
$row = mysql_fetch_array( $sql );
$old_ip = $row['current_ip'];

My problem is, I am getting incorrect results from:

if ($old = $new)
{
$different = '1';    
}
else {
$different = '2';  
}
echo $different;

I ALWAYS get 2, wether the IPs are the same or not; if I use '==' as the comparison, I ALWAYS get 1.

When I run the following code, I get the following output: var_dump($old_ip); var_dump($new_ip);

Output

string(15) "123.123.123.123" string(167) "184.6.216.163 "

Are the variables different types? If So, can I make them the same so I am only comparing the IP and not the type? If the IPs are the same I should get '1' and if they are different, I should get '2', right?

2
  • You are getting string(167) on var_dump($new_ip)? Are you sure there is nothing else in that string as 184.6.216.163 is only 13. Commented Jun 28, 2013 at 5:07
  • btw. you are using a deprecated extension mysql Commented Jun 28, 2013 at 5:12

5 Answers 5

4

Your problem is lurking here:

string(15) "123.123.123.123" string(167) "184.6.216.163 "
                                    ^^^

The address retrieved from your database is coming along with 154 unwanted characters. trim() both strings and then do the comparison.

Oh - and you should really shorten the field that stores the IP address. 15 characters should do.

Sign up to request clarification or add additional context in comments.

2 Comments

The number on the left is coming from the database, the one on the right, is from the $new_ip = file_get_contents('ipaddresscheck.comlu.com/ip.php');
@JustinMarmorato Sorry - my mistake. I lost track of where the values were coming from in the midst of a phone call. Nevertheless, the value I highlighted is still way too long. Take a look at the code that creates it.
2

Code

if ($old = $new) // you are using assignment operator here
{
   $different = '1';    
}

Should be

if ($old == $new) //Equal to operator
{
   $different = '1';    
}

Also confirm the variable names are $old and $new or $old_ip and $new_ip

2 Comments

Quoted from the OP: if I use '==' as the comparison, I ALWAYS get 1.. So there is more to it.
it should be $old_ip == $new_ip... you dont have $old variable in the code provided its $old_ip and $new_ip
1
  • Assignment ( = ): Assigns the value on the right to the variable on the left
  • Equality ( == ): Checks if the left and right values are equal
  • Identical ( === ): Checks if the left and right values are equal AND identical (same variable type)

    Example:

    $a = 1; // Sets the value of $a as the integer 1
    $b = TRUE; // Sets the value of $b to the boolean TRUE
    if ($a == $b){
    echo 'a is equal to b.';
    }
    
    if ($a === $b){ // compare VALUE and data type: if $a(integer) === $b(boolean)
    echo 'a is identical and equal to b.';
    }
    
    if ($a = $b){ 
    echo '$b value to variable $a';
    }
    

In your code i noticed a problem. A space is there in second IP address. so

string(15) "123.123.123.123" string(167) "184.6.216.163 "
                                                       ^
  1. Trim both IP address.
  2. If not work, then convert it to other datatype and compare.(Just for debugging purpose. :))

4 Comments

how do I strip the whitespace? Ive tried $old_ip3 = str_replace(' ', '', $old_ip2); $new_ip3 = str_replace(' ', '', $new_ip2);
Build-in function are there to trim PHP string. $old_ip2 = trim($old_ip);
Ive tried trim and str_replace. I can't seem to get the extra hundred something characters out of that variable. Is there a way I cam limit the size of the variable? That would be a start, then I would only have to figure out how to deal with the space when the IP is not 3.3.3.3, say 2.3.1.3.
You could use regular expression to strip your IP from the rest. Use preg_match() to find a match for a IP address and return the match, or use preg_replace() and replace all unwanted characters.
0

OLD :

if ($old = $new){
   $different = '1';    
}

New Correct :

if ($old == $new){
   $different = '1';    
}

you are using (=) assignment operation in the place of comparison operation.

1 Comment

I actually did that before I posted the question. The question was a typo. But regardless, it still does not work.
0

As far as I can see the behaviour is normal because you are using $old and $new both are unset.

$new_ip = file_get_contents('http://www.ipaddresscheck.comlu.com/ip.php');

$sql = mysql_query('SELECT * FROM ip ORDER BY  id DESC LIMIT 1');
$row = mysql_fetch_array( $sql );
$old_ip = $row['current_ip'];

The next code will always give 2 because $new is unset. What you are doing here is assigning $old the value of $new and since $new is unset $old = $new will always be false.

if ($old = $new)
{
    $different = '1';    
} else {
    $different = '2';  
}
echo $different;

In the next example you are actually comparing $old and $new. Since both are unset the expression $old == $new will always be true.

if ($old == $new)
{
    $different = '1';    
} else {
    $different = '2';  
}
echo $different;

What you need to do is using the right vars and trim them.

// get new ip and trim white spaces
$new_ip = trim(file_get_contents('http://www.ipaddresscheck.comlu.com/ip.php'));
// get the old ip
$sql = mysql_query('SELECT * FROM ip ORDER BY  id DESC LIMIT 1');
$row = mysql_fetch_array( $sql );
$old_ip = $row['current_ip'];

if ($old_ip == $new_ip)
{
    $different = '1';    
} else {
    $different = '2';  
}
echo $different;

I am not trimming white spaces on $old_ip because I guess that you will be using $new_ip to update your database and since $new_ip is already trimmed we can be sure that $old_ip has no white spaces.

You should always have php warnings enabled when coding! It would have popped you a warning because you used unset vars.

PS : note that this code will only work as expected if file_get_contents() returns a IP with white spaces, the output could be different (error output, not able to detect ip correctly, ... etc)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.