Search code examples
phphttpipworking-directorycreate-directory

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


I need create folders to all users who visiting my website using their ip address. the folder name should be "user_ipaddress". so i have this kind of code. it's creates this kind of folders "user_1" and "user_127001". so is this right way to create new folder to individual user. or not.? thank you.

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);

Solution

  • I'm Checked this on online web server it's working perfectly. thank you

    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);