简体   繁体   English

如何发送用户名/密码到htaccess?

[英]How to send username/password to htaccess?

There is a way to send username and password to directories protected by .htaccess/htpasswd, using php? 有一种方法可以使用PHP将用户名和密码发送到受.htaccess / htpasswd保护的目录中? Rather than having to enter the username and password in that horrible pop up? 不必在该可怕的弹出窗口中输入用户名和密码?

I have a login page "outside" the area protected by htaccess / htpasswd, and would like to access a file "inside" of my protected by htaccess / htpasswd directory by entering the username and password in my php script, rather than enter in the pop up that appears when i try to access the protected area. 我在htaccess / htpasswd保护的区域“外部”有一个登录页面,并且想通过在php脚本中输入用户名和密码来访问htaccess / htpasswd保护的目录“内部”文件,而不是在我尝试访问保护区时出现的弹出窗口。

Is there any way to do this using both, php and htaccess? 有没有办法同时使用php和htaccess?

To expand on my comment, consider the following PHP script, which reads the protected file and gives it to the user if the proper password foobar is supplied. 为了进一步说明我的意见,请考虑以下PHP脚本,如果提供了正确的密码foobar ,它将读取受保护的文件并将其提供给用户。

Keep in mind that if your connection is not over SSL , you are sending your password in the clear. 请记住,如果您的连接不是通过SSL ,那么您将以明文形式发送密码。

   <?php

    $pass = $_POST['pass'];
    $filename = "MyFoo.txt";

    $file = "path/to/protected/directory/$filename";

    if($pass == "foobar")
    {
        header('Content-type: application/octet-stream');
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
    }
    if(isset($_POST))
    {?>
        <form method="POST" action="">
            Password <input type="password" name="pass"></input>
            <input type="submit" name="submit" value="Get File" ></input>
            </form>
    <?}
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM