dtc4547 2011-04-11 03:14
浏览 35
已采纳

密码使用Apache2和PHP独立保护单个PHP文件

Dear folks, Inside folder images I would like to password protect a single php file render.php I already have in that folder my .htaccess as well as my .htpasswd files however they dont work

.htaccess in folder /images

// does not work ??
<Files render.php>
  AuthName "Login"
  AuthType Basic
  AuthUserFile /var/www/vhosts/site.com/httpdocs/images/.htpasswd
  require valid-user
</FilesMatch>

.htpasswd

admin:818jp2uNLY6ZW     
# generated with http://www.4webhelp.net/us/password.php

As soon as I set the rules in htaccess of that folder, all stuff in that folder seems get weirdly processed, css files insite that folder get corrupted etc. Why does this not work ?

PS I am also perfectly fine (in fact would prefer, if possible at all) to use a password setting within php file itself, that way I will be sure nothing else gets affected than only that php file no matter its name or location ( probably more elegant and timess ) though I saw everyone using htaccess so there must be an advantage there, right?

Thanks very much for your suggestions!

  • 写回答

1条回答 默认 最新

  • drti52047 2011-04-11 03:43
    关注

    You can perform this auth in PHP as shown in the manual:

    <?php
    if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] != 'admin' || $_SERVER['PHP_AUTH_PW'] != 'foobar') {
        header('WWW-Authenticate: Basic realm="My Realm"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Unauthorized';
        exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?