热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Boa服务器中如何使用cgi管理cookie(c语言)

1【方法1】(1)设置cookieprintf(Set-Cookie:username%s;path;\r\n,username);printf(Content-type:texthtml\n\n);注意:设置cookie的语句要放在Content-type前,否则不能生效。(2)查看cookieprintf(Content-type:texthtml\n\n);pri

1

【方法1】

(1) 设置COOKIE

  1. printf( \"Set-COOKIE:username=%s; path=/; rn \",username);  
  2.   
  3. printf(\"Content-type: text/htmlnn\");  

注意:设置COOKIE的语句要放在Content-type前,否则不能生效。

(2) 查看COOKIE

  1. printf(\"Content-type: text/htmlnn\");  
  2. printf(\"<html>n\");  
  3.       
  4. info=getenv(\"HTTP_COOKIE\");  
  5. if(info!=NULL)  
  6. {  
  7.       sscanf(info,\"username=%s\",username);  
  8. }  

注意:HTTP_COOKIE而不是HTTP-COOKIE,很多网络资料上都写错了,结果查到的COOKIE数据为空。

【方法2】

(1) 设置COOKIE

  1. printf(\"<head>n\");  
  2. printf(\"<script  charset=\"gb2312\" language=\"Javascript\" >n\");                  
  3. printf(\"function setCOOKIE(c_name,value,expiredays)                   
  4.     n{                                       
  5.         nvar exdate=new Date();                          
  6.         nexdate.setDate(exdate.getDate()+expiredays)                 
  7.         ndocument.COOKIE=c_name+ \"=\" +escape(value)+               
  8.         n((expiredays==null) ? \"\" : \"; expires=\"+exdate.toGMTString());      
  9.     }n\");                                        
  10. printf(\"script>n\");  
  11. printf(\"head>n\");  

在标签中调用:

  1. printf(\"<script type=\"text/Javascript\">n\");  
  2. printf(\"setCOOKIE(\'username\',\'%s\',1);n\",username);  
  3. //printf(\"window.location.href=\"xxxx\";n\");  
  4. printf(\"script>n\");  

(2) 获取COOKIE

  1. printf(\"<head>n\");  
  2. printf(\"<script  charset=\"gb2312\" language=\"Javascript\" >n\");                  
  3. printf(\"function getCOOKIE(c_name)                                
  4. n{                                               
  5. n  if(document.COOKIE.length>0)                               
  6. n  {                                             
  7. n      c_start=document.COOKIE.indexOf(c_name + \"=\");                  
  8. n      if(c_start!=-1)                                   
  9. n      {                                         
  10. n          c_startc_start=c_start + c_name.length+1;                    
  11. n          c_end=document.COOKIE.indexOf(\";\",c_start);                 
  12. n          if(c_end==-1) c_end=document.COOKIE.length;               
  13. n          return unescape(document.COOKIE.substring(c_start,c_end));        
  14. n      }                                         
  15. n}                                               
  16. n  return \"\";                                          
  17. n}n\");                                      
推荐阅读
author-avatar
水晶玲珑9261996
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有