【问题标题】:I need create a new folder when visiting new users into my website using thier ip - php使用他们的 ip - php 将新用户访问到我的网站时,我需要创建一个新文件夹
【发布时间】:2021-02-27 22:41:53
【问题描述】:

我需要为所有使用他们的 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);

【问题讨论】:

  • 你可以使用 is_dir ; is_dir doc
  • “这是创建新文件夹的正确方法”。好不好用?
  • 这是工作。谢谢

标签: php http ip working-directory create-directory


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多