当前位置:Linux教程 - Php - 请问php里怎么得到访客的ip和端口号

请问php里怎么得到访客的ip和端口号

function getip(){
                        if (isset($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])&&$HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]){
                                $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
                        } elseif(isset($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])&&$HTTP_SERVER_VARS["HTTP_CLIENT_IP"]){
                                $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
                        } elseif(isset($HTTP_SERVER_VARS["REMOTE_ADDR"])&&$HTTP_SERVER_VARS["REMOTE_ADDR"]) {
                                $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
                        } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
                                $ip = getenv("HTTP_X_FORWARDED_FOR");
                        } elseif (getenv("HTTP_CLIENT_IP")) {
                                $ip = getenv("HTTP_CLIENT_IP");
                        } elseif (getenv("REMOTE_ADDR")) {
                                $ip = getenv("REMOTE_ADDR");
                        } else {
                                $ip="Unknown";
                        }
                        return $ip;
        }