Password protected directory and files in PHP

前端 未结 3 998
盖世英雄少女心
盖世英雄少女心 2021-02-06 16:13

Im creating a simple private page with links to some files to download. I\'ve done it with simple session management but I have a problem: if somebody click on the file-url he c

3条回答
  •  伪装坚强ぢ
    2021-02-06 16:46

    Here is a very simple solution using htpasswd (quick and easy and it works).

    $ cd /path/to/password_protected_dir
    $ vi .htaccess
    

    Then within your .htaccess file you will need to add this:

    AuthUserFile /path/to/.htpasswd
    AuthGroupFile /dev/null
    AuthName "My Private Directory"
    AuthType Basic
    
    
    require valid-user
    
    

    Next you will generate the .htpasswd file which you just included in the .htaccess file as show above. You can do this like so:

    $ htpasswd -c .htpasswd user_name
    

    At this point you will be prompted to enter the password.

    Of course, this is merely one method of accomplishing this.

提交回复
热议问题