亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 Apache服務(wù)器的用戶認(rèn)證 (轉(zhuǎn))

Apache服務(wù)器的用戶認(rèn)證 (轉(zhuǎn))

Jun 21, 2016 am 09:14 AM
apache mysql php quot

apache|服務(wù)器

經(jīng)常上網(wǎng)的讀者會(huì)遇到這種情況:訪問一些網(wǎng)站的某些資源時(shí),瀏覽器彈出一個(gè)對話框,要求輸入用戶名和密碼來獲取對資源的訪問。這就是用戶認(rèn)證的一種技術(shù)。用戶認(rèn)證是保護(hù)網(wǎng)絡(luò)系統(tǒng)資源的第一道防線,它控制著所有登錄并檢查訪問用戶的合法性,其目標(biāo)是僅讓合法用戶以合法的權(quán)限訪問網(wǎng)絡(luò)系統(tǒng)的資源?;镜挠脩粽J(rèn)證技術(shù)是“用戶名+密碼”。


  Apache是目前流行的Web服務(wù)器,可運(yùn)行在Linux、Unix、Windows等操作系統(tǒng)下,它可以很好地解決“用戶名+密碼”的認(rèn)證問題。Apache用戶認(rèn)證所需要的用戶名和密碼有兩種不同的存貯方式:一種是文本文件;另一種是MSQL、Oracle、MySQL等數(shù)據(jù)庫。下面以Linux的Apache為例,就這兩種存貯方式,分別介紹如何實(shí)現(xiàn)用戶認(rèn)證功能,同時(shí)對Windows的Apache用戶認(rèn)證作簡要的說明。

  采用文本文件存儲(chǔ)

  這種認(rèn)證方式的基本思想是:Apache啟動(dòng)認(rèn)證功能后,就可以在需要限制訪問的目錄下建立一個(gè)名為.htaccess的文件,指定認(rèn)證的配置命令。當(dāng)用戶第一次訪問該目錄的文件時(shí),瀏覽器會(huì)顯示一個(gè)對話框,要求輸入用戶名和密碼,進(jìn)行用戶身份的確認(rèn)。若是合法用戶,則顯示所訪問的頁面內(nèi)容,此后訪問該目錄的每個(gè)頁面,瀏覽器自動(dòng)送出用戶名和密碼,不用再輸入了,直到關(guān)閉瀏覽器為止。以下是實(shí)現(xiàn)的具體步驟:

  以超級(jí)用戶root進(jìn)入Linux,假設(shè)Apache 1.3.12已經(jīng)編譯、安裝到了/usr/local/apache目錄中。缺省情況下,編譯Apache時(shí)自動(dòng)加入mod_auth模塊,利用此模塊可以實(shí)現(xiàn)“用戶名+密碼”以文本文件為存儲(chǔ)方式的認(rèn)證功能。

  1.修改Apache的配置文件/usr/local/apache/conf/httpd.conf,對認(rèn)證資源所在的目錄設(shè)定配置命令。下例是對/usr/local/apache/htdocs/members目錄的配置:

 ?。糄irectory /usr/local/apache/htdocs /members>

  Options Indexes FollowSymLinks

  allowoverride authconfig

  order allow,deny

  allow from all

 ?。?Directory>

  其中,allowoverride authconfig一行表示允許對/usr/local/apache/htdocs/ members目錄下的文件進(jìn)行用戶認(rèn)證。

  2.在限制訪問的目錄/usr/local/apache/htdocs/members下建立一個(gè)文件.htaccess,其內(nèi)容如下:

  AuthName "會(huì)員區(qū)"

  AuthType basic

  AuthUserFile/usr/local/apache/members.txt

  require valid-user

  說明:文件.htaccess中常用的配置命令有以下幾個(gè):

  1) AuthName命令:指定認(rèn)證區(qū)域名稱。區(qū)域名稱是在提示要求認(rèn)證的對話框中顯示給用戶的(見附圖)。

  2)AuthType命令:指定認(rèn)證類型。在HTTP1.0中,只有一種認(rèn)證類型:basic。在HTTP1.1中有幾種認(rèn)證類型,如:MD5。

  3) AuthUserFile命令:指定一個(gè)包含用戶名和密碼的文本文件,每行一對。

  4) AuthGroupFile命令:指定包含用戶組清單和這些組的成員清單的文本文件。組的成員之間用空格分開,如:

  managers:user1 user2

  5) require命令:指定哪些用戶或組才能被授權(quán)訪問。如:

  require user user1 user2(只有用戶user1和user2可以訪問)

  requiresgroupsmanagers (只有組managers中成員可以訪問)

  require valid-user (在AuthUserFile指定的文件中任何用戶都可以訪問)

  3.利用Apache附帶的程序htpasswd,生成包含用戶名和密碼的文本文件:/usr/local/apache/members.txt,每行內(nèi)容格式為“用戶名:密碼”。

  #cd /usr/local/apache/bin

  #htpasswd -bc ../members.txt user1 1234

  #htpasswd -b ../members.txt user2 5678

  文本文件members.txt含有兩個(gè)用戶:user1,口令為1234;user2,口令為5678。注意,不要將此文本文件存放在Web文檔的目錄樹中,以免被用戶下載。

  欲了解htpasswd程序的幫助,請執(zhí)行htpasswd -h。

  當(dāng)用戶數(shù)量比較少時(shí),這種方法對用戶的認(rèn)證是方便、省事的,維護(hù)工作也簡單。但是在用戶數(shù)量有數(shù)萬人,甚至數(shù)十萬人時(shí),會(huì)在查找用戶上花掉一定時(shí)間,從而降低服務(wù)器的效率。這種情形,應(yīng)采用數(shù)據(jù)庫方式。

  采用數(shù)據(jù)庫存儲(chǔ)

  目前,Apache、PHP4、MySQL三者是Linux下構(gòu)建Web網(wǎng)站的最佳搭檔,這三個(gè)軟件都是免費(fèi)軟件。將三者結(jié)合起來,通過HTTP協(xié)議,利用PHP4和MySQL,實(shí)現(xiàn)Apache的用戶認(rèn)證功能。

  只有在PHP4以Apache的模塊方式來運(yùn)行的時(shí)候才能進(jìn)行用戶認(rèn)證。為此,在編譯Apache時(shí)需要加入PHP4模塊一起編譯。假設(shè)PHP4作為Apache的模塊,編譯、安裝Apache到/usr/local/apache目錄,編譯、安裝MySQL到/usr/local/mysql目錄。然后進(jìn)行下面的步驟:

  1.在MySQL中建立一個(gè)數(shù)據(jù)庫member,在其中建立一個(gè)表users,用來存放合法用戶的用戶名和密碼。

  1)用vi命令在/tmp目錄建立一個(gè)SQL腳本文件auth.sql,內(nèi)容為:

  drop database if exists member;

  create database member;

  use member;

  create table users (

  username char(20) not null,

  password char(20) not null,

  );

  insertsintosusers values("user1",password("1234"));

  insertsintosusers values("user2",password("5678"));

  2)啟動(dòng)MySQL客戶程序mysql,執(zhí)行上述SQL腳本文件auth.sql的命令,在表users中增加兩個(gè)用戶的記錄。

  #mysql -u root -pmypwd</tmp/auth.sql

  2.編寫一個(gè)PHP腳本頭文件auth.inc,程序內(nèi)容為:

 ?。?php

  function authenticate() {

  Header('WWW-authenticate: basic realm="會(huì)員區(qū)"');

  Header('HTTP/1.0 401 Unauthorized');

  echo "你必須輸入正確的用戶名和口令。 ";

  exit;

  }

  function CheckUser(, ) {

  if ( == "" || == "") return 0;

   = "SELECT username,password FROM usersswheresusername='' and password=password('')";

   = mysql_connect('localhost', 'root', 'mypwd');

  mysql_select_db('member',);

   = mysql_query(, );

  =mysql_num_rows();

  mysql_close();

  if (>0) {

  return 1; //有效登錄

  } else {

  return 0; //無效登錄

  }

  }

  ?>

  函數(shù)Authenticate()的作用是利用函數(shù)Header('WWW-authenticate: basic realm="會(huì)員區(qū)"'),向?yàn)g覽器發(fā)送一個(gè)認(rèn)證請求消息,使瀏覽器彈出一個(gè)用戶名/密碼的對話框。當(dāng)用戶輸入用戶名和密碼后,包含此PHP腳本的URL將自動(dòng)地被再次調(diào)用,將用戶名、密碼、認(rèn)證類型分別存放到PHP4的三個(gè)特殊變量:、、,在PHP程序中可根據(jù)這三個(gè)變量值來判斷是否合法用戶。Header()函數(shù)中,basic表示基本認(rèn)證類型,realm的值表示認(rèn)證區(qū)域名稱。

  函數(shù)Header('HTTP/1.0 401 Unauthorized')使瀏覽器用戶在連續(xù)多次輸入錯(cuò)誤的用戶名或密碼時(shí)接收到HTTP 401錯(cuò)誤。

  函數(shù)CheckUser()用來判斷瀏覽器用戶發(fā)送來的用戶名、密碼是否與MySQL數(shù)據(jù)庫的相同,若相同則返回1,否則返回0。其中mysql_connect('localhost', 'root', 'mypwd')的數(shù)據(jù)庫用戶名root和密碼mypwd,應(yīng)根據(jù)自己的MySQL設(shè)置而改變。

  3.在需要限制訪問的每個(gè)PHP腳本程序開頭增加下列程序段:

 ?。?php

  require('auth.inc');

  if (CheckUser(,)==0) {

  authenticate();

  } else {

  echo "這是合法用戶要訪問的網(wǎng)頁。"; //將此行改為向合法用戶輸出的網(wǎng)頁

  }

  ?>

  把需要向合法用戶顯示的網(wǎng)頁內(nèi)容放到else子句中,取代上述程序段的一行:

  echo "這是合法用戶要訪問的網(wǎng)頁。";

  這樣,當(dāng)用戶訪問該P(yáng)HP腳本程序時(shí),需要輸入用戶名和密碼來確認(rèn)用戶的身份。

  Windows的Apache用戶認(rèn)證

  1.采用文本文件存放用戶名和密碼時(shí),其方法同前,但需要注意的是表示路徑的目錄名之間、目錄名與文件名之間一律用斜線“/”分開,而不是反斜線“”。

  2.采用MySQL數(shù)據(jù)庫存放用戶名和密碼時(shí),首先按下列方法將PHP 4.0.3作為Apache的模塊來運(yùn)行,然后按上述“采用數(shù)據(jù)庫存儲(chǔ)用戶名和密碼的用戶認(rèn)證”的方法完成。

  1)下載Windows版的Apache 1.3.12、PHP 4.0.3、MySQL 3.2.32,將三個(gè)軟件分別解壓、安裝到C:pache、C:PHP4、C:mysql目錄。

  2) C:PHP4SAPI目錄有幾個(gè)常用Web服務(wù)器的PHP模塊文件,將其中php4apache.dll拷貝到Apache的modules子目錄(C:pachemodules)。

  3)修改Apache的配置文件C:pachenfhttpd.conf,增加以下幾行:

  LoadModule php4_module modules/ php4apache.dll

  AddType application/x-httpd-php .php3

  AddType application/x-httpd-php-source .phps

  AddType application/x-httpd-php .php

  第一行使PHP4以Apache的模塊方式運(yùn)行,這樣才能進(jìn)行用戶認(rèn)證,后三行定義PHP腳本程序的擴(kuò)展名。

  4)在autoexec.bat文件的PATH命令中增加PHP4所在路徑“C:PHP4”,重新啟動(dòng)電腦。


經(jīng)我測試,2.0版本的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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 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

PHP Tutorial
1488
72
Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Object-Relational Mapping (ORM) Performance Tuning in PHP Object-Relational Mapping (ORM) Performance Tuning in PHP Jul 29, 2025 am 05:00 AM

Avoid N 1 query problems, reduce the number of database queries by loading associated data in advance; 2. Select only the required fields to avoid loading complete entities to save memory and bandwidth; 3. Use cache strategies reasonably, such as Doctrine's secondary cache or Redis cache high-frequency query results; 4. Optimize the entity life cycle and call clear() regularly to free up memory to prevent memory overflow; 5. Ensure that the database index exists and analyze the generated SQL statements to avoid inefficient queries; 6. Disable automatic change tracking in scenarios where changes are not required, and use arrays or lightweight modes to improve performance. Correct use of ORM requires combining SQL monitoring, caching, batch processing and appropriate optimization to ensure application performance while maintaining development efficiency.

Building Resilient Microservices with PHP and RabbitMQ Building Resilient Microservices with PHP and RabbitMQ Jul 27, 2025 am 04:32 AM

To build a flexible PHP microservice, you need to use RabbitMQ to achieve asynchronous communication, 1. Decouple the service through message queues to avoid cascade failures; 2. Configure persistent queues, persistent messages, release confirmation and manual ACK to ensure reliability; 3. Use exponential backoff retry, TTL and dead letter queue security processing failures; 4. Use tools such as supervisord to protect consumer processes and enable heartbeat mechanisms to ensure service health; and ultimately realize the ability of the system to continuously operate in failures.

Creating Production-Ready Docker Environments for PHP Creating Production-Ready Docker Environments for PHP Jul 27, 2025 am 04:32 AM

Using the correct PHP basic image and configuring a secure, performance-optimized Docker environment is the key to achieving production ready. 1. Select php:8.3-fpm-alpine as the basic image to reduce the attack surface and improve performance; 2. Disable dangerous functions through custom php.ini, turn off error display, and enable Opcache and JIT to enhance security and performance; 3. Use Nginx as the reverse proxy to restrict access to sensitive files and correctly forward PHP requests to PHP-FPM; 4. Use multi-stage optimization images to remove development dependencies, and set up non-root users to run containers; 5. Optional Supervisord to manage multiple processes such as cron; 6. Verify that no sensitive information leakage before deployment

A Deep Dive into PHP's Internal Garbage Collection Mechanism A Deep Dive into PHP's Internal Garbage Collection Mechanism Jul 28, 2025 am 04:44 AM

PHP's garbage collection mechanism is based on reference counting, but circular references need to be processed by a periodic circular garbage collector; 1. Reference count releases memory immediately when there is no reference to the variable; 2. Reference reference causes memory to be unable to be automatically released, and it depends on GC to detect and clean it; 3. GC is triggered when the "possible root" zval reaches the threshold or manually calls gc_collect_cycles(); 4. Long-term running PHP applications should monitor gc_status() and call gc_collect_cycles() in time to avoid memory leakage; 5. Best practices include avoiding circular references, using gc_disable() to optimize performance key areas, and dereference objects through the ORM's clear() method.

Building Immutable Objects in PHP with Readonly Properties Building Immutable Objects in PHP with Readonly Properties Jul 30, 2025 am 05:40 AM

ReadonlypropertiesinPHP8.2canonlybeassignedonceintheconstructororatdeclarationandcannotbemodifiedafterward,enforcingimmutabilityatthelanguagelevel.2.Toachievedeepimmutability,wrapmutabletypeslikearraysinArrayObjectorusecustomimmutablecollectionssucha

The Serverless Revolution: Deploying Scalable PHP Applications with Bref The Serverless Revolution: Deploying Scalable PHP Applications with Bref Jul 28, 2025 am 04:39 AM

Bref enables PHP developers to build scalable, cost-effective applications without managing servers. 1.Bref brings PHP to AWSLambda by providing an optimized PHP runtime layer, supports PHP8.3 and other versions, and seamlessly integrates with frameworks such as Laravel and Symfony; 2. The deployment steps include: installing Bref using Composer, configuring serverless.yml to define functions and events, such as HTTP endpoints and Artisan commands; 3. Execute serverlessdeploy command to complete the deployment, automatically configure APIGateway and generate access URLs; 4. For Lambda restrictions, Bref provides solutions.

python check if key exists in dictionary example python check if key exists in dictionary example Jul 27, 2025 am 03:08 AM

It is recommended to use the in keyword to check whether a key exists in the dictionary, because it is concise, efficient and highly readable; 2. It is not recommended to use the get() method to determine whether the key exists, because it will be misjudged when the key exists but the value is None; 3. You can use the keys() method, but it is redundant, because in defaults to check the key; 4. When you need to get a value and the expected key usually exists, you can use try-except to catch the KeyError exception. The most recommended method is to use the in keyword, which is both safe and efficient, and is not affected by the value of None, which is suitable for most scenarios.

See all articles