寫一個輸入密碼的網頁 check.html 主要部分就是
</div>
<div class="content">
<h3><font color ="gray">請輸入管理員密碼</h3>
<div><input name="password" type="password" /></div>
</br>
<div><input type="button" value="確定" οnclick="loadconfig()"/></div>
</div>
loadconfig()函數
function loadconfig()
{
var code = 1009;
netconfigfrm.actioncode.value = code;
netconfigfrm.submit();
}
此網頁的功能就是將密碼傳給服務器,供cgi程序處理;
CGI部分
CGI處理密碼的函數是:
static void set_cookie(){
char password[32]={0};
cgiFormString("password",password,32);
if(strcmp(password,"123456")==0) //比較密碼
{
htmlHeader("The temp page");
htmlBody();
fprintf(cgiOut,"<SCRIPT LANGUAGE='JavaScript'>");
fprintf(cgiOut, "parent.document.cookie = 'password=123456';");//123456就是密碼 cookie的有效時間爲直到瀏覽器關閉
fprintf(cgiOut, "parent.window.location.href ='../index.html';");//cookie設置成功後跳轉到index.html 這裏前面加了一個../ ,是因爲此時是在虛擬目錄
fprintf(cgiOut,"</SCRIPT>"); // ./cgi-bin下 如果直接寫index.html 那麼URL地址就是 http://ip地址/cgi-bin/index.html 那麼就會報錯, // http://ip地址/index.html 纔是希望得到的
htmlFooter();
}
else
{
htmlHeader("The temp page");
htmlBody();
fprintf(cgiOut,"<SCRIPT LANGUAGE='JavaScript'>");
fprintf(cgiOut, "alert('密碼錯誤')");
fprintf(cgiOut,"</SCRIPT>");
htmlFooter();
}
}
在其他網頁上的JavaScript加上以下兩個函數:
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
function checkCookie()
{
username=getCookie('password')
if (username!=null && username!="")
return 0;
else
window.location.href="check.html"
}
在<body>你認爲合適的 地方 (一般是最開始的地方)加上以下代碼:
<SCRIPT LANGUAGE="JavaScript">
checkCookie();
</SCRIPT>
檢查是否設置了cookie,若沒有設置就轉到check.html,若設置了(即驗證過密碼了)就return 0.當然 這裏的密碼沒有經過加密處理,,有心人可能通過抓包得到密碼,可以在腳本處設置加密步驟
希望可以幫助到大家
PS:在這之前想用boa的驗證用戶功能 ,對比boa-0.93.16,修改boa-0.94.14rc21裏面的源代碼,使得boa-0.94.14rc21版能夠實現驗證用戶,但是一直沒有成功,每次啓動boa服務器的使用,報錯 can't recognise keyword “Auth”; 顯然還是沒有處理配置文件新加的 關鍵字 Auth 不知道有木有同志成功,望交流。。-^-