2011-06-01 83 views
0

简单的登录我想创建一个只有几个硬编码的用户名/密码连击简单的登录它可以访问个人的私有目录与我上传文件。 (mysite.com/user1/,mysite.com/anotheruser/...)私人用户文件

下面是我mylogin.php的内容。

“密码保护”的下载文件只有从这个页面,并通过各自的用户是什么样的方法有我吗?这些信息不会非常敏感,但应该有点安全。

<?php //List of users and their passwords 
$users = array(
    "myusername" => "mYpaSSw0rd2011", 
    "anothername" => "password2" 
);?> 

<html> 
<head><title>Private Login</title></head> 
<body> 

    <form method="POST" action="mylogin.php"> 
     Username: <input type="text" name="username" size="15" /><br /> 
     Password: <input type="password" name="password" size="15" /><br /> 
     <input type="submit" value="Login" /> 
    </form> 

<?php //check username:password combo 
if (isset($_POST["username"]) && $_POST["username"] != "" && ($users[$_POST["username"]] == $_POST["password"]) 
    //******************************************************* 
    //list (private) files in mysite.com/username's directory 
    //*******************************************************  
}?> 

</body></html> 

回答

2

你会发现这是非常有用的:

标题( '内容处置:附件; filename.pdf');

只需读取文件(例如使用readfile),转储数据并将标题设置为附件。

商店里无法通过HTTP访问(或使用.htaccess的保护他们)原件。

+0

还好吧!我能够得到它的工作。花了一分钟的时间来围绕header()函数和文件/目录函数,但是你的建议让我指出了正确的方向。 – 2011-06-02 16:14:30

0

我想这可能是适当的张贴我结束了使用以供将来参考的代码。我最终将项目分成3个不同的文件(以及用户的目录)。

在用户目录中,我放置了带有<Files *>Deny from all</Files>的.htaccess文件,以便保护它们的文件。

另外,我知道我做了一些这方面的东西很糟糕。我开始使用会话,但我很难让它正常工作。你可以看到代码中的一些残余。任何帮助重构将不胜感激。

我剪掉了一些标记,以使下面的代码更具可读性。

================================================================== 
index.php========================================================= 
================================================================== 

    <form method="POST" action="userview.php"> 
     Username: <input type="text" name="username" size="15" /><br /> 
     Password: <input type="password" name="password" size="15" /><br /> 
     <input type="submit" value="Login" /> 
    </form> 

==================================================================  
userview.php====================================================== 
================================================================== 

<?php 
    //List of users and their passwords 
    $users = array(
     "myusername" => "mYpaSSw0rd2011", 
     "anothername" => "password2", 
    ); 

    //Check posted user:pass & start session (or die on mismatch) 
     //****Session currently doesn't carry over to download.php. I am re-posting username with a hidden form field**** 
    if (isset($_POST["username"]) && $_POST["username"] != "" && ($users[$_POST["username"]] == $_POST["password"])) { 
     session_start(); 
     $_SESSION['user'] = $_POST["username"]; 
    } else die("incorrect login"); 
?> 

<html><head><title><?php echo $_POST["username"] ?>'s Files</title></head><body> 
<h1>Contents of <?php echo $_POST["username"] ?>'s Directory:</h1> 

<?php 
    $handle = opendir($_SESSION['user'] . '/'); 
    if ($handle) { //display directory contents within a radio button list. 
     echo '<form method="POST" action="download.php">'; 
     echo '<input type="hidden" name="user" value="' . $_SESSION['user'] . '">'; 
     echo '<fieldset>'; 
     while (false !== ($file = readdir($handle))) { 
      if (substr($file, 0, 1) !== ".") 
       echo 
        "<label><input type=\"radio\" name=\"dlfile\" value=\"$file\" /> $file", "</label><br />\n"; 
     } 
     closedir($handle); 
     echo '</fieldset><br /><input type="submit" value="Download" /></form>' , "\n\n"; 
    } else die("error: Please contact administrator"); 
?> 
</body></html> 

================================================================== 
download.php====================================================== 
================================================================== 

<?php 
if (isset($_POST['user']) && isset($_POST['dlfile'])) { 
    $file = $_POST['user'] . "/" . $_POST['dlfile']; 
    if (file_exists($file)) { 
     header('Content-Description: File Transfer'); 
     header('Content-Type: text/plain'); 
     header('Content-Disposition: attachment; filename='.basename($file)); 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($file)); 
     ob_clean(); 
     flush(); 
     readfile($file); 
     exit; 
    } 
} 

?>