-
การสร้าง password สำหรับ protect การเข้าถึง directory หรือหน้า Web
Posted on April 28th, 2011 No commentsสามารถทำได้โดยการใช้ .htaccess
เช่นต้องการสร้างให้ user ชื่อ myGuest สามารถ เข้าใช้งาน directory ชื่อ /public-html/pweb
โดยใช้ password ว่า “helloworld”วิธีการดังนี้
1. สร้าง password file (ใช้ชื่ออะไรก็ได้เช่นในตัวอย่างนี้ต้องการใช้ชื่อว่า PasswdFIle แต่อาจสร้างเป็น hidden file ก็ได้เช่นตั้งชื่อว่า .htpasswd)
Anan@Moose$ htpasswd -c PasswdFile myGuest
New password: <— ใส่ password ที่ต้องการ ในที่นี้คือ helloworld
Re-type new password: <— ใส่ helloworld อีกครั้ง
Adding password for user myGuestถ้าทดลองดู file ที่มีชื่อว่า PasswdFile จะพบ
Anan@Moose$ cat PasswdFile
myGuest:.AaK93J3nybioP <— ตัวอย่างข้อมูลใน password file ที่ถูกเข้ารหัสตาม password ที่ตั้งไว้
2. สร้าง .htaccess file
Anan@Moose$ cd public-html/pweb <— ย้ายไป directory ที่ต้องการ protect
Anan@Moose:~/public-html/pweb$ vi .htaccessAuthType Basic
AuthName “restricted area” <—- ระวังอักษรตัว quote เป็นฟันหนูคู่ ” ” (ถ้าผิดระบบอาจฟ้อง Internal Server Error)
AuthUserFile /home/Anan/PasswdFile
require valid-user
ServerSignature Off2. ทดสอบดู โดยการพยายามเข้าที่หน้านั้น จะพบว่าให้ใส่ username และ password
ก็ให้ใส่ myGuest และ helloworld ก็จะมองเห็น file ใน directory ที่ต้องการ protect
แต่ถ้าใส่ผิด จะถาม username และ password โดยไม่ยอมให้เข้าครับ