国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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

Apache服務器的用戶認證 (轉(zhuǎn))

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

apache|服務器

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


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

  采用文本文件存儲

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

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

  1.修改Apache的配置文件/usr/local/apache/conf/httpd.conf,對認證資源所在的目錄設定配置命令。下例是對/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目錄下的文件進行用戶認證。

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

  AuthName "會員區(qū)"

  AuthType basic

  AuthUserFile/usr/local/apache/members.txt

  require valid-user

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

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

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

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

  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含有兩個用戶:user1,口令為1234;user2,口令為5678。注意,不要將此文本文件存放在Web文檔的目錄樹中,以免被用戶下載。

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

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

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

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

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

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

  1)用vi命令在/tmp目錄建立一個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)啟動MySQL客戶程序mysql,執(zhí)行上述SQL腳本文件auth.sql的命令,在表users中增加兩個用戶的記錄。

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

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

 ?。?php

  function authenticate() {

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

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

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

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

 ?。?php

  require('auth.inc');

  if (CheckUser(,)==0) {

  authenticate();

  } else {

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

  }

  ?>

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

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

  這樣,當用戶訪問該PHP腳本程序時,需要輸入用戶名和密碼來確認用戶的身份。

  Windows的Apache用戶認證

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

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

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

  2) C:PHP4SAPI目錄有幾個常用Web服務器的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的模塊方式運行,這樣才能進行用戶認證,后三行定義PHP腳本程序的擴展名。

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


經(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)

A Simple Guide to PHP Setup A Simple Guide to PHP Setup Jul 18, 2025 am 04:25 AM

The key to setting up PHP is to clarify the installation method, configure php.ini, connect to the web server and enable necessary extensions. 1. Install PHP: Use apt for Linux, Homebrew for Mac, and XAMPP recommended for Windows; 2. Configure php.ini: Adjust error reports, upload restrictions, etc. and restart the server; 3. Use web server: Apache uses mod_php, Nginx uses PHP-FPM; 4. Install commonly used extensions: such as mysqli, json, mbstring, etc. to support full functions.

Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Learning PHP: A Beginner's Guide Learning PHP: A Beginner's Guide Jul 18, 2025 am 04:54 AM

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

Writing Effective PHP Comments Writing Effective PHP Comments Jul 18, 2025 am 04:44 AM

Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

Mastering PHP Block Comments Mastering PHP Block Comments Jul 18, 2025 am 04:35 AM

PHPblockcommentsareusefulforwritingmulti-lineexplanations,temporarilydisablingcode,andgeneratingdocumentation.Theyshouldnotbenestedorleftunclosed.BlockcommentshelpindocumentingfunctionswithPHPDoc,whichtoolslikePhpStormuseforauto-completionanderrorche

Quick PHP Installation Tutorial Quick PHP Installation Tutorial Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

Improving Readability with Comments Improving Readability with Comments Jul 18, 2025 am 04:46 AM

The key to writing good comments is to explain "why" rather than just "what was done" to improve the readability of the code. 1. Comments should explain logical reasons, such as considerations behind value selection or processing; 2. Use paragraph annotations for complex logic to summarize the overall idea of functions or algorithms; 3. Regularly maintain comments to ensure consistency with the code, avoid misleading, and delete outdated content if necessary; 4. Synchronously check comments when reviewing the code, and record public logic through documents to reduce the burden of code comments.

Effective PHP Commenting Effective PHP Commenting Jul 18, 2025 am 04:33 AM

The key to writing PHP comments is clear, useful and concise. 1. Comments should explain the intention behind the code rather than just describing the code itself, such as explaining the logical purpose of complex conditional judgments; 2. Add comments to key scenarios such as magic values, old code compatibility, API interfaces, etc. to improve readability; 3. Avoid duplicate code content, keep it concise and specific, and use standard formats such as PHPDoc; 4. Comments should be updated synchronously with the code to ensure accuracy. Good comments should be thought from the perspective of others, reduce the cost of understanding, and become a code understanding navigation device.

See all articles