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

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

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

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

apache|服務(wù)器

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


  Apache是目前流行的Web服務(wù)器,可運行在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,假設(shè)Apache 1.3.12已經(jīng)編譯、安裝到了/usr/local/apache目錄中。缺省情況下,編譯Apache時自動加入mod_auth模塊,利用此模塊可以實現(xiàn)“用戶名+密碼”以文本文件為存儲方式的認證功能。

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

  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ù)十萬人時,會在查找用戶上花掉一定時間,從而降低服務(wù)器的效率。這種情形,應(yīng)采用數(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模塊一起編譯。假設(shè)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,應(yīng)根據(jù)自己的MySQL設(shè)置而改變。

  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服務(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的模塊方式運行,這樣才能進行用戶認證,后三行定義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)

How to handle File Uploads securely in PHP? How to handle File Uploads securely in PHP? Jul 08, 2025 am 02:37 AM

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

How Do You Pass Variables by Value vs. by Reference in PHP? How Do You Pass Variables by Value vs. by Reference in PHP? Jul 08, 2025 am 02:42 AM

InPHP,variablesarepassedbyvaluebydefault,meaningfunctionsorassignmentsreceiveacopyofthedata,whilepassingbyreferenceallowsmodificationstoaffecttheoriginalvariable.1.Whenpassingbyvalue,changestothecopydonotimpacttheoriginal,asshownwhenassigning$b=$aorp

Implementing Transactions and Understanding ACID Properties in MySQL Implementing Transactions and Understanding ACID Properties in MySQL Jul 08, 2025 am 02:50 AM

MySQL supports transaction processing, and uses the InnoDB storage engine to ensure data consistency and integrity. 1. Transactions are a set of SQL operations, either all succeed or all fail to roll back; 2. ACID attributes include atomicity, consistency, isolation and persistence; 3. The statements that manually control transactions are STARTTRANSACTION, COMMIT and ROLLBACK; 4. The four isolation levels include read not committed, read submitted, repeatable read and serialization; 5. Use transactions correctly to avoid long-term operation, turn off automatic commits, and reasonably handle locks and exceptions. Through these mechanisms, MySQL can achieve high reliability and concurrent control.

Handling character sets and collations issues in MySQL Handling character sets and collations issues in MySQL Jul 08, 2025 am 02:51 AM

Character set and sorting rules issues are common when cross-platform migration or multi-person development, resulting in garbled code or inconsistent query. There are three core solutions: First, check and unify the character set of database, table, and fields to utf8mb4, view through SHOWCREATEDATABASE/TABLE, and modify it with ALTER statement; second, specify the utf8mb4 character set when the client connects, and set it in connection parameters or execute SETNAMES; third, select the sorting rules reasonably, and recommend using utf8mb4_unicode_ci to ensure the accuracy of comparison and sorting, and specify or modify it through ALTER when building the library and table.

PHP find the position of the last occurrence of a substring PHP find the position of the last occurrence of a substring Jul 09, 2025 am 02:49 AM

The most direct way to find the last occurrence of a substring in PHP is to use the strrpos() function. 1. Use strrpos() function to directly obtain the index of the last occurrence of the substring in the main string. If it is not found, it returns false. The syntax is strrpos($haystack,$needle,$offset=0). 2. If you need to ignore case, you can use the strripos() function to implement case-insensitive search. 3. For multi-byte characters such as Chinese, the mb_strrpos() function in the mbstring extension should be used to ensure that the character position is returned instead of the byte position. 4. Note that strrpos() returns f

Connecting to MySQL Database Using the Command Line Client Connecting to MySQL Database Using the Command Line Client Jul 07, 2025 am 01:50 AM

The most direct way to connect to MySQL database is to use the command line client. First enter the mysql-u username -p and enter the password correctly to enter the interactive interface; if you connect to the remote database, you need to add the -h parameter to specify the host address. Secondly, you can directly switch to a specific database or execute SQL files when logging in, such as mysql-u username-p database name or mysql-u username-p database name

Designing a Robust MySQL Database Backup Strategy Designing a Robust MySQL Database Backup Strategy Jul 08, 2025 am 02:45 AM

To design a reliable MySQL backup solution, 1. First, clarify RTO and RPO indicators, and determine the backup frequency and method based on the acceptable downtime and data loss range of the business; 2. Adopt a hybrid backup strategy, combining logical backup (such as mysqldump), physical backup (such as PerconaXtraBackup) and binary log (binlog), to achieve rapid recovery and minimum data loss; 3. Test the recovery process regularly to ensure the effectiveness of the backup and be familiar with the recovery operations; 4. Pay attention to storage security, including off-site storage, encryption protection, version retention policy and backup task monitoring.

Windows automatic repair loop fix Windows automatic repair loop fix Jul 07, 2025 am 01:31 AM

Use the installation media to enter the recovery environment; 2. Run the bootrec command to repair the boot records; 3. Check for disk errors and repair system files; 4. Disable automatic repair as a temporary means. The Windows automatic repair loop is usually caused by system files corruption, hard disk errors or boot configuration abnormalities. The solution includes troubleshooting by installing the USB flash drive into the recovery environment, using bootrec to repair MBR and BCD, running chkdsk and DISM/sfc to repair disk and system files. If it is invalid, the automatic repair function can be temporarily disabled, but the root cause needs to be checked later to ensure that the hard disk and boot structure are normal.

See all articles