作者:水晶玲珑9261996 | 来源:互联网 | 2017-11-02 15:38
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
- printf( \"Set-COOKIE:username=%s; path=/; rn \",username);
-
- printf(\"Content-type: text/htmlnn\");
注意:设置COOKIE的语句要放在Content-type前,否则不能生效。
(2) 查看COOKIE
- printf(\"Content-type: text/htmlnn\");
- printf(\"<html>n\");
-
- info=getenv(\"HTTP_COOKIE\");
- if(info!=NULL)
- {
- sscanf(info,\"username=%s\",username);
- }
注意:HTTP_COOKIE而不是HTTP-COOKIE,很多网络资料上都写错了,结果查到的COOKIE数据为空。
【方法2】
(1) 设置COOKIE
- printf(\"<head>n\");
- printf(\"<script charset=\"gb2312\" language=\"Javascript\" >n\");
- printf(\"function setCOOKIE(c_name,value,expiredays)
- n{
- nvar exdate=new Date();
- nexdate.setDate(exdate.getDate()+expiredays)
- ndocument.COOKIE=c_name+ \"=\" +escape(value)+
- n((expiredays==null) ? \"\" : \"; expires=\"+exdate.toGMTString());
- }n\");
- printf(\"script>n\");
- printf(\"head>n\");
在标签中调用:
- printf(\"<script type=\"text/Javascript\">n\");
- printf(\"setCOOKIE(\'username\',\'%s\',1);n\",username);
- //printf(\"window.location.href=\"xxxx\";n\");
- printf(\"script>n\");
(2) 获取COOKIE
- printf(\"<head>n\");
- printf(\"<script charset=\"gb2312\" language=\"Javascript\" >n\");
- printf(\"function getCOOKIE(c_name)
- n{
- n if(document.COOKIE.length>0)
- n {
- n c_start=document.COOKIE.indexOf(c_name + \"=\");
- n if(c_start!=-1)
- n {
- n c_startc_start=c_start + c_name.length+1;
- n c_end=document.COOKIE.indexOf(\";\",c_start);
- n if(c_end==-1) c_end=document.COOKIE.length;
- n return unescape(document.COOKIE.substring(c_start,c_end));
- n }
- n}
- n return \"\";
- n}n\");