Search code examples
phpapache.htaccess.htpasswd

require username and password


Please guide me accordingly if this question is noobish or a kind of duplicate.

I am wondering how to implement this kind of authentication?

enter image description here

I am asking because I'm quite lost and doesn't know which keywords to search for this. On my mind, I am planning to have this whenever a user wants to access my domain (eg. http://mysite.com and http://mysite.com/tools).


Solution

  • The keywords you are looking for are: htaccess, htpasswd, .htaccess. (Providing you use apache)

    Ultimately you will put a .htaccess file in the dir you want to protect, and let it check the requirements with your htpasswdfile.

    For example: on my server I don't want people seeing my private directory.

    $ sudo htpasswd -c /etc/apache2/.htpasswd benjamin
    

    Note that benjamin in the command above ^ is the username which we will use to log in.

    We'd like the .htpasswd file outside our webfolder for some security.

    Then in my private dir I'd put a .htaccess file that contains this:

    AuthUserFile /etc/apache2/.htpasswd
    AuthGroupFile /dev/null
    AuthName Authenticate
    AuthType Basic
    require user benjamin