| 9.74 User authentication ด้วย .htpasswd + .htaccess : การปิดห้อง แต่ยอมให้เข้าด้วย username และ password ที่กำหนด
 อ่านเพิ่มเติมได้จาก http://www.apacheweek.com/features/userauth หรือ http://www.thaiall.com/cgi/htpasswd.pl
 ขั้นตอนการ lock ห้องของตนเอง ในฐานะผู้ใช้ทั่วไป
ท่านต้องส่งแฟ้ม 2 แฟ้มคือ .htpasswd และ .htaccess ไปไว้ในห้องที่ต้องการ lock โดยทำการถาม User และ password ก่อนเข้า
.htaccess นั้นสามารถคัดลอกด้านล่างไปได้เลย โดยแก้ไขอะไรเล็กน้อยตามต้องการ เช่นเปลี่ยนคำว่า lock ที่บรรทัดแรก เป็น username ของท่าน
.htpasswd แต่ละบรรทัดคือ 1 user สำหรับแฟ้มนี้ ถ้าผู้ดูแลไม่คิดบริการ ท่านก็ใช้บริการไม่ได้ สำหรับ server ที่ผมดูแล ได้ทำ โปรแกรมเข้ารหัส สร้างรหัสให้
ถ้าเป็นผู้ดูแล(Super user) สามารถใช้
 คำสั่ง #htpasswd -nb yourname yourpassword แสดงรหัสผ่าน ที่ shell prompt
 คำสั่ง #htpasswd -c .htpasswd yourname สร้างแฟ้ม .htpasswd พร้อม รอรับรหัสผ่าน จากแป้นพิมพ์
 ดูเพิ่มเติมจาก #man htpasswd ที่ shell prompt
เปิดเว็บ http://www.isinthai.com/lock/index.php เพื่อป้อนรหัสผู้ใช้ และรหัสผ่าน จะได้รหัสที่สร้างขึ้น เพื่อนำไปเพิ่มในแฟ้ม .htpasswd
 http://www.isinthai.com/lock/.htaccess
AuthUserFile /var/www/html/lock/.htpasswd
http://www.isinthai.com/lock/.htpasswdAuthName "User:yourname Password:yourpassword for accessing this directory"
 AuthType Basic
 require valid-user
 DirectoryIndex index.html index.htm index.shtml index.php
 test:1A/c8vPQJQiL2
ปัญหา และวิธีแก้ไข ที่ผู้ดูแลอาจพบyourname:UtecEDcEa3/L2
 ครั้งแรกที่สร้าง .htaccess และ .htpasswd ก็ไม่เกิดผล คือสองแฟ้มนี้ไม่ทำงาน ตรวจสอบแล้วพบว่าผู้ดูแลต้องแก้แฟ้ม /etc/httpd/conf/httpd.conf
 ให้ส่วนของ <Directory "/var/www/html"> ในบรรทัดที่เขียนว่า AllowOverride None เปลี่ยนเป็น AllowOverride All
 หรือส่วนของ <Directory "/"> ในบรรทัดที่เขียนว่า AllowOverride None เปลี่ยนเป็น AllowOverride All ถ้าท่านใช้ public_html
 http://www.isinthai.com/lock/index.php 
:: ใช้แสดงรหัสที่ผู้ใช้กำหนดขึ้น และนำไปเพิ่มในแฟ้ม .htpasswd ของ directory ที่ต้องการ lock
<form action=index.php method=get>
User: <input name=u value=yourname><br>
Password: <input name=p value=yourpassword><br>
<input type=submit value=generate_password>
</form>
This line for .htpasswd<br><font face='TLWGTypewriter'>
<?
$x = "htpasswd -nb ". $_GET['u'] . " " . $_GET['p'];
echo `$x`;
?>
<hr>
Detail of .htaccess and .htpasswd at<br>
http://www.thaiall.com/cgi/htpasswd.pl 
 |