cgic程序小技巧

一、cgi程序实现跳转页面


printf("");


fprintf(cgiOut, "\n");//限时


fprintf(cgiOut, "\n");//马上


二、cgi程序登录后保存信息到cookies然后html通过js获得cookies判断后再显示页面,否则回到登录界面


1、cgi设置cookies
giHeaderCookieSetString("user",USER,60,cgiScriptName,"192.168.164.131");//保存一分钟,最后一个是域名;
这个函数是cgic库的API
使用这个函数一定要在cgiHeaderContentType("text/html");这个语句之前
2、cgi获取cookies
if(cgiCookies(&array) != cgiFormSuccess)
printf("cookies get fail\n");
arrayStep = array;
fprintf(cgiOut, "\n");
fprintf(cgiOut, "\n");
while (*arrayStep) {
char value[1024];
fprintf(cgiOut, "");
fprintf(cgiOut, "
CookieValue
");
cgiHtmlEscape(*arrayStep);
fprintf(cgiOut, "
");
cgiCookieString(*arrayStep, value, sizeof(value));
fprintf(cgiOut, "\n");
cgiHtmlEscape(value);
arrayStep++;
}
cgiStringArrayFree(array);

3、删除cookies
giHeaderCookieSetString("user",USER,0,cgiScriptName,"192.168.164.131");
将时间设置为0,也要在cgiHeaderContentType("text/html");语句前


3.JS中操作cookie


//创建cookie
function SetCookie(sName, sValue)
{
 document.cookie = sName + "=" + escape(sValue) + "; ";
}


//获取
function GetCookie(sName)
{
 var aCookie = document.cookie.split("; ");
 for (var i=0; i < aCookie.length; i++)
 {
  var aCrumb = aCookie[i].split("=");
  if (sName == aCrumb[0])
  return unescape(aCrumb[1]);
 }
}
//删除
function DelCookie(sName)
{
document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}
//检查cookies
function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
{
alert('Welcome again '+username+'!')
}
else 
{
 username=prompt('Please enter your name:',"")
 if (username!=null && username!="")
{
setCookie('username',username,365)
}
}
}


js要获取cgic设置的cookies要将cookies放在"/"路径
giHeaderCookieSetString("user",USER,60,"/","192.168.164.131");//保存一分钟,最后一个是域名;



你可能感兴趣的:(嵌入式软件,C语言,cookies,cgi,user,function,null,border)