Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get Correct IP #21

Open
MDW-Chris opened this issue Sep 23, 2016 · 0 comments
Open

get Correct IP #21

MDW-Chris opened this issue Sep 23, 2016 · 0 comments

Comments

@MDW-Chris
Copy link

J'ai capté un bug autour de la récupération de l'adresse IP.
Il se peut que la directive $_SERVER[HTTP_X_FORWARDED_FOR] retourne 2 adresses IP. La réponse dans mon cas prenait la forme de :
92.xxx.xxx.xxx, 10.10.xxx.xxx

Pour contourner ce bug, je me suis inspiré de ce post
http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php?answertab=votes#tab-top

Cela donne une fonction que j'ai ainsi utilisé partout dans votre code ou il y avait une recherche d'adresse IP courante.

function get_ip_address(){
    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
        if (array_key_exists($key, $_SERVER) === true){
            foreach (explode(',', $_SERVER[$key]) as $ip){
                $ip = trim($ip); // just to be safe
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
                    return $ip;
                }
            }
        }
    }
}

Bonne correction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant