cgic程序小技巧

一、cgi程序实现跳转页面


printf("<meta http-equiv=\"Refresh\" content=\"0;URL=/index.html\">");


fprintf(cgiOut, "<script>window.setTimeout(\"location.href = '../login.html'\", 5000);</script>\n");//限时


fprintf(cgiOut, "<script>window.location.href = '../login.html';</script>\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, "<table border=1>\n");
fprintf(cgiOut, "<tr><th>Cookie<th>Value</tr>\n");
while (*arrayStep) {
char value[1024];
fprintf(cgiOut, "<tr>");
fprintf(cgiOut, "<td>");
cgiHtmlEscape(*arrayStep);
fprintf(cgiOut, "<td>");
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");//保存一分钟,最后一个是域名;



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值