Home php教程 php手册 我来写个详细的: Win32下具体实现Apache的用户验证(原创)

我来写个详细的: Win32下具体实现Apache的用户验证(原创)

Jun 21, 2016 am 09:12 AM
apache localhost test user users

apache|原创

Win32下具体实现Apache的用户验证
相信大家采用的验证方法很多种,而在Win32环境下如何设置Apache用户验证的介绍很少,手册上介绍的也是Unix和linux环境下的配置,象我这样在win32下苦苦摸索Apache的人应该不少,我根据自己的经验介绍一下自己是如何在Win32环境下实现apache的用户验证功能的。

方法一:以单用户方式实现某目录只能由某个用户或几个用户访问。
假设Apache的web根目录为 d:/home,对应URL为 http://localhost/
如欲使d:/home/test目录只能由某用户访问,该目录对应URL为http://localhost/test,访问该URL时需要弹出验证对话框。
d:/users目录用来存放密码文件 user.passwd

1、制作用户密码文件
在命令行方式先用Apache自带的工具htpasswd.exe生成一个密码文件叫user.passwd,-c参数表示新建文件,无参数表示在该口令文件中以添加方式增加一个用户。
D:\apache\bin>htpasswd -bc d:/users/user.passwd user1 123456
这样,就生成了d:/users/user.passwd 口令文件,里面有一行文字 user1:$apr1$4S3.....$.su.8AIrDEYMX7jKv2RT9/
成功增加了一个user1的用户,如果想再添加其他用户,就采用追加方式添加用户:
D:\apache\bin>htpasswd -b d:/users/user.passwd user2 123456
D:\apache\bin>htpasswd -b d:/users/user.passwd user3 123456

好了,当添加完用户,就可以着手实现用户验证了。

2、制作访问验证文件 .htaccess
打开记事本,输入以下文字:

authtype basic
authname "www.home.net"
authuserfile d:/users/users.passwd
require user user1

然后另存为 .htaccess,保存路径为你想实现验证的目录:d:/home/test/下,注意保存类型选"所有文件",文件名为".htaccess",否则就不会生成 .htaccess 文件了。

到这里,指定某个用户验证的设置就完成了,在浏览器中输入验证目录所对应的地址 http://localhost/test/,会发现浏览器弹出一个对话框要求你输入用户名和口令,你就只能输入用户user1和密码12345才能进去,输入其他用户如user2,user3是没用的。

3、实现指定某几个用户对一个目录的验证
例如决定 http://localhost/test/ 能由user1和user3访问,user2不能访问,则编辑 d:/home/test/.htaccess 文件,把最后的一句:
require user user1
改为:
require user user1 user3
如此就实现了 http://localhost/test/ 只能由user1和user2访问

4、实现所有合法用户对一个目录的验证
例如决定 http://localhost/test/ 能由 d:/users/user.passwd 文件里的所有用户访问,则编辑 d:/home/test/.htaccess 文件,把最后的一句改为 :

require valid-user

就可以实现在 user.passwd 里用户都能访问http://localhost/test/了,只要输入的用户名和密码没有错误。

方法二:采用群组用户方式实现验证
要实现其实也很简单,在方法一中对 user.passwd 文件里的用户进行分组,建立用户组文件,再在 .htaccess 文件里指定 authgroupfile 就可以了。假设 user.passwd 中已经添加了很多用户 (user1,user2,user3,user4,...)
1、建立一个用户组文件 d:/users/user.group ,内容如下:

manager:user1 user3
game:user2 user4
download:user5 user6 user7

如此,user.group 文件里把 user1至user7这7个用户分为了3个组:manager,game,download 。

2、实现某组用户能访问 http://localhost/test/
编辑 d:/home/test/.htaccess 文件,内容如下:

authtype basic
authname "www.home.net"
authuserfile d:/users/users.passwd
authgroupfile d:/users/user.group
require group game

那么,http://localhost/test/就只能由 game组里的用户 user2,user4 才能访问了。

以上步骤,对于熟悉编程开发的同志来说,完全可以做个图形界面程序来完成,就省事多了。
至此,Apache在Win32环境下的简单验证就实现了。看来和 Linux下的设置方法差不多,我却走了不少弯路才摸索出来,惭愧惭愧啊。
实现Apache的验证方法还有数种途径,以上只是一方面而已。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

How to view your apache version How to view your apache version Apr 13, 2025 pm 01:15 PM

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to view the apache version How to view the apache version Apr 13, 2025 pm 01:00 PM

How to view the Apache version? Start the Apache server: Use sudo service apache2 start to start the server. View version number: Use one of the following methods to view version: Command line: Run the apache2 -v command. Server Status Page: Access the default port of the Apache server (usually 80) in a web browser, and the version information is displayed at the bottom of the page.

How to configure zend for apache How to configure zend for apache Apr 13, 2025 pm 12:57 PM

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

How to solve the problem that apache cannot be started How to solve the problem that apache cannot be started Apr 13, 2025 pm 01:21 PM

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

How to delete more than server names of apache How to delete more than server names of apache Apr 13, 2025 pm 01:09 PM

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

See all articles