当使用他们的 ip 访问新用户到我的网站时,我需要创建一个新文件夹 - php

I need create a new folder when visiting new users into my website using thier ip - php

我需要为所有使用他们的 IP 地址访问我的网站的用户创建文件夹。文件夹名称应为“user_ipaddress”。所以我有这种代码。它创建了这种文件夹“user_1”和“user_127001”。这是为个人用户创建新文件夹的正确方法。或不。?谢谢。

public function getRealIpAddr(){

    //whether ip is from the share internet  
    if(!empty($_SERVER['HTTP_CLIENT_IP'])) {  

        $ip = $_SERVER['HTTP_CLIENT_IP'];  

    }  
    //whether ip is from the proxy  
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 

                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
    }  
        //whether ip is from the remote address  
    else{  
            $ip = $_SERVER['REMOTE_ADDR'];  
    }  
    $ip = str_replace(array(":","."), "", $ip);
    return $ip;  

}

public function createDir($ip){

    $file_name = "../data/user_" . $ip;

    if(file_exists($file_name)){

        return $file_name;

    }else{
        
        mkdir($file_name);
        return $file_name;
    }
}

$ip = $this->getRealIpAddr();

createDir($ip);

我在在线网络服务器上检查过它运行良好。谢谢

public function getRealIpAddr(){

//whether ip is from the share internet  
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {  

    $ip = $_SERVER['HTTP_CLIENT_IP'];  

}  
//whether ip is from the proxy  
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 

            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
}  
    //whether ip is from the remote address  
else{  
        $ip = $_SERVER['REMOTE_ADDR'];  
}  
$ip = str_replace(array(":","."), "", $ip);
return $ip;  

}

public function createDir($ip){

$file_name = "../data/user_" . $ip;

if(file_exists($file_name)){

    return $file_name;

}else{
    
    mkdir($file_name);
    return $file_name;
}
}

$ip = $this->getRealIpAddr();

createDir($ip);