Often see this phenomenon, see
Apache Page access Control
Why this kind of control, to different people to see different things, the protection of information, although this protection is relatively low, more or less a bit of use.
One, with the HTPASSWD command, generate permission control file
[Zhangy@blackghost test]$ htpasswd-c./access tank //Generate a password file,-C is to create a new file htpasswd-h view new password: // Prompt for password re-type new password: //Repeat password adding password for user tank [zhangy@blackghost test]$ cat access // Check the password file Tank:uj5b3qif/bndi //The user name is plaintext and the password is encrypted.
[Zhangy@blackghost test]$ htpasswd-c./access tank //Generate a password file,-C is to create a new file htpasswd-h view new password: // Prompt to enter password re-type new password: //Repeat password adding password for user tank[zhangy@blackghost test]$ cat access // Check the password file Tank:uj5b3qif/bndi //The user name is plaintext and the password is encrypted.
The password file is generated here.
Second, page access control method
1, can be modified httpd.conf or httpd-vhosts.conf to configure
Listen 10004 namevirtualhost *:10004 <virtualhost *:10004> documentroot "/home/zhangy/www/test" ServerName *:10004 bandwidthmodule on forcebandwidthmodule in Bandwidth all 1024000 minbandwidth all 50000 Largefilelimit * 50000 maxconnection all 2 errorlog "/home/zhangy/apache/ Blog.51yip.com.com-error.log "Customlog"/home/zhangy/apache/blog.51yip.com-access.log "common //look at the configuration below <Directory/home/zhangy/www/test> authtype Basic authname "Access test" authuserfile/home/zhangy/ Www/test/access Require valid-user </Directory> </VirtualHost>
Listen 10004NameVirtualHost *:10004<virtualhost *:10004> documentroot "/home/zhangy/www/test" ServerName * : 10004 bandwidthmodule on Forcebandwidthmodule in Bandwidth all 1024000 minbandwidth all 50000 largefilelimit * 500 50000 Maxconnection all 2 Errorlog "/home/zhangy/apache/blog.51yip.com.com-error.log" Customlog "/home/zhangy/apache/ Blog.51yip.com-access.log "common//Look, the following configuration <Directory/home/zhangy/www/test> authtype Basic authname" Access Test "authuserfile/home/zhangy/www/test/access Require valid-user </Directory></VirtualHost>
2, we can use the. htaccess file for control
Build A. htaccess file under the root directory of test
[Zhangy@blackghost test]$ vi. htaccess //Open files, add permission contents [Zhangy@blackghost test]$ Cat. htaccess // Here is the content of. htaccess authtype Basic authname "Access test" authuserfile/home/zhangy/www/test/access Require Valid-user
[Zhangy@blackghost test]$ vi. htaccess //Open files, add permission contents [Zhangy@blackghost test]$ Cat. htaccess // Here is the content of. htaccess authtype Basic authname "Access test" authuserfile/home/zhangy/www/test/access Require Valid-user
If you want to set a different user name and password for many people, it is convenient to use the authgroupfile setting option
3, no password files, access control is also possible
-
define (' admin_username ', ' tank '); Admin usernamedefine (' Admin_password ', ' tank '); Admin Password//log Checkif (!isset ($_server[' Php_auth_user ']) | |!isset ($_server[' PHP_AUTH_PW ') | | $_server[' Php_auth_user ']! = Admin_username | | $_server[' PHP_AUTH_PW ']! = Admin_password) {Header ("Www-authenticate:basic realm=/" Access test/"); Header ("http/1.0 401 Unauthorized"); echo <<<eob
Define (' Admin_username ', ' tank '); Admin usernamedefine (' Admin_password ', ' tank '); Admin Password//log Checkif (!isset ($_server[' Php_auth_user ']) | |!isset ($_server[' PHP_AUTH_PW ']) | | $_server[' PHP_ Auth_User ']! = Admin_username | | $_server[' PHP_AUTH_PW ']! = Admin_password) {Header ("Www-authenticate:basic realm=/" Access test/"); Header ("http/1.0 401 Unauthorized"); echo <<<eob
The PHP method is used above, and I think there are other languages. You can write the above code into a file, share the inclusion, or encapsulate it to the base so that you can access it regardless of the page you visit.