0

I use this function and Its Fine on Local means when $hexIpStr exist function return false otherwise return true, But when I Deploy my Project on Server this Function only Return True, $arr is 2D array.

function ipChecker($arr, $hexIp)
{
    $hexIpStr = "HEX=" . $hexIp;
    foreach ($arr as $members) {
        if (in_array($hexIpStr, $members)) {
            return false;
        } else {
            return true;
        }
    }
}

The function is for example called with these values:

$hexIp = 'f528764d624db129b32c21fbca0cb8d6';
$arr = array(
  0 => [
    0 => 'FullName=mehdi',
    1 => '[email protected]',
    2 => 'IP=127.0.0.1',
    3 => 'HEX=f528764d624db129b32c21fbca0cb8d6',
    4 => '=>',
    5 => 'Opinion=1 ',
  ],
  1 => [
    0 => '',
  ],
);

no its not answer because its fine in the server i get this

FullName=asd [email protected] IP=192.119.160.197 HEX=dd029394f038b0775138a23df8d9cddd => Opinion=1 and its right and i use this function to get user ip:

function GetRealIp()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
 }
    return $ip;}
8
  • What did it return on local ? If it returns true, it only means that the variable $hexIpStr holding the data is not found in the array $members. Commented Sep 2, 2018 at 5:50
  • no,the only different is local and server Commented Sep 2, 2018 at 5:54
  • 1
    Can you var_dump($hexIpStr); and var_dump(members); to check if $hexIpStr is actually found in the array members. Commented Sep 2, 2018 at 5:58
  • 1
    Did you do the var_dump() on the server ? (should not be done on the local as it is working fine there). With the code above, the only reason it is returning true it because the $hexIpStr is not present in $members. Commented Sep 2, 2018 at 6:11
  • 1
    put your $arr output here. Commented Sep 2, 2018 at 7:14

2 Answers 2

1

Your function "returns early" (and thus breaks the foreach) after checking the first child for your HEX=... string. This means you only ever check the first member.

Given this $arr, your member is in there, just not at the first position:

array(
  0 => [
    0 => 'FullName=blub',
    1 => '[email protected]',
    2 => 'IP=1.2.3.4',
    3 => 'HEX=6465ec74397c9126916786bbcd6d7601',
  ],
  1 => [
    0 => 'FullName=mehdi',
    1 => '[email protected]',
    2 => 'IP=127.0.0.1',
    3 => 'HEX=f528764d624db129b32c21fbca0cb8d6',
  ],
)

It would return true, because only in_array('HEX=f528764d624db129b32c21fbca0cb8d6', ['HEX=6465ec74397c9126916786bbcd6d7601']) is checked.

You should restructure your function so it can only "return early" if it found your member, and if not, continues to check all members, and returns the other case only in the end:

function ipChecker($arr, $hexIp)
{
    $hexIpStr = "HEX=" . $hexIp;
    foreach ($arr as $members) {
        if (in_array($hexIpStr, $members)) {
            return false;
        }
    }
    return true;
}
Sign up to request clarification or add additional context in comments.

Comments

0

f528764d624db129b32c21fbca0cb8d6(#) is md5('127.0.0.1'), this is likely not the value (IP) you expect / that is in $members. You likely expect a public IP.

If you rely on $_SERVER['REMOTE_ADDR'] this can be the issue. If your server is proxying the request to your PHP worker on the same machine (rather than using e.g. mod_php directly), REMOTE_ADDR will be the IP of the proxy (127.0.0.1/localhost) rather than the visitor.

In this case, you should dump $_SERVER and check for a variable HTTP_X_FORWARDED_FOR / use that instead of REMOTE_ADDR, the proxy server will likely add this to include the original IP.

2 Comments

No its not the answer explain why in the question
Thanks for updating the question. Yeah, it was a guess. After seeing your $arr, I found a more likely issue with the loop. Check stackoverflow.com/a/52134997/7362396

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.