Retrieve local IP address in PHP
Solved
Hello,
I’d like to know how to display the local IP address and determine whether it is static or dynamic.
The context is that I’m programming an intranet app that retrieves user information and redirects based on that information.
Thank you.
I’d like to know how to display the local IP address and determine whether it is static or dynamic.
The context is that I’m programming an intranet app that retrieves user information and redirects based on that information.
Thank you.
4 answers
-
ModeratorHello, To retrieve an IP there aren’t 36 solutions...
<?php /** * Retrieve the real IP address of a visitor */ function get_ip() { // IP if shared internet if (isset($_SERVER['HTTP_CLIENT_IP'])) { return $_SERVER['HTTP_CLIENT_IP']; } // IP behind a proxy elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { return $_SERVER['HTTP_X_FORWARDED_FOR']; } // Otherwise: normal IP else { return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''); } } ?>but regarding whether it is dynamic or static... I don’t know any way to determine it. (apart possibly, comparing the user’s IP on each visit to see if it matches the previous one... assuming they don’t change computers to connect...) -- Best regards, Jordane -
okay thanks.
But I found this function
gethostbyname(gethostname());
but to determine if it’s static or not, not yet. -
Trying the code you gave me, it returns this result
::1 -
9a doesn't work; I only get the IP address.