君子意如何

我就是路人甲 http://www.dongdongyou.com/ 动动友

导航

保留代码 ,cookies控制类

 

public static class WriteCookies
{

    
public static readonly string CookiesName = "GetCookiesCoolectName";

    
/// <summary>
    
/// 写独立cookie项目
    
/// </summary>

    public static bool SetCookie(string CookiesName, string CookiesValue)
    
{
        
try
        
{
            HttpContext.Current.Response.Cookies.Add(
new HttpCookie(CookiesName, CookiesValue));
            
return true;
        }

        
catch
        
{
            
return false;
        }

    }


    
/// <summary>
    
/// 读独立cookie
    
/// </summary>

    public static string GetCookie(string CookiesName)
    
{
        
return HttpContext.Current.Request.Cookies[CookiesName].Value;
    }



    
/// <summary>
    
/// 取得某个cookies集合的字段函数
    
/// </summary>

    public static string GetCookies(string CookiesName)
    
{
        HttpContext ht 
= HttpContext.Current;
        HttpCookie mytest 
= ht.Request.Cookies[CookiesName];
        
if (ht.Request.Cookies[CookiesKey] == null)
            
return null;
        
else
            
return mytest.Values[CookiesName].ToString();
    }



    
/// <summary>
    
/// 写cookies集合字段函数
    
/// </summary>

    public static bool SetCookies(string CookiesName, string CookiesValue)
    
{
        
try
        
{
            HttpContext ht 
= HttpContext.Current;
            HttpCookie mytest 
= ht.Request.Cookies[CookiesName];
            
if (ht.Response.Cookies[CookiesKey].HasKeys)
            
{
                
bool ishave = false;
                
foreach (string name in mytest.Values.AllKeys)
                
{
                    
if (name == CookiesName)
                    
{
                        mytest.Values[CookiesName] 
= CookiesValue;
                        ishave 
= true;
                        
break;
                    }

                }

                
if (!ishave)
                    mytest.Values.Add(CookiesName, CookiesValue);
                ht.Response.AppendCookie(mytest);

            }

            
else
            
{
                ht.Response.Cookies[CookiesKey].Values.Add(CookiesName, CookiesValue);
                ht.Response.AppendCookie(ht.Response.Cookies[CookiesKey]);
            }

            
return true;
        }

        
catch
        
{
            
return false;
        }

    }



}



 下面是js 代码

   
   
//获得默认城市的ID,要是没有cookies那么获取头中的id,在写cookies
   function GetDefaultCityID()
   
{    
        
var domain=escape(document.domain);
        
var DefaultCityId = "UnionDefaultCityIdShow";
        
var CityIdValue =GetCookie(DefaultCityId); 
        
if(CityIdValue==null||CityIdValue=="")
        
{   
            
try
            
{
                CityIdValue 
= wwwUnion58ComDefaultCityId;
            }

            
catch(e)
            
{
                CityIdValue 
= 1;
            }
   
            
var DefaultCityId = "UnionDefaultCityIdShow";
            setCookie(DefaultCityId,CityIdValue,
"","",domain,"");
            setCookie(
"wwwUnion58ComDefaultCityName","北京","","",domain,"");    
        }

        
return CityIdValue;
   }

   
   
//获得默认城市名称,没有返回默认的北京
   function GetDefaultCityName()
   
{    
        
var domain=escape(document.domain);
        
var DefaultCityId = "wwwUnion58ComDefaultCityName";
        
var CityIdValue =GetCookie(DefaultCityId); 
        
if(CityIdValue==null||CityIdValue=="")
        
{   
            
try
            
{
                CityIdValue 
= wwwUnion58ComDefaultCityName;
            }

            
catch(e)
            
{
                CityIdValue 
="北京";
            }
   
            
//setCookie("wwwUnion58ComDefaultCityName","北京","","",domain,"");    
        }

        
return CityIdValue;
   }

   
   
//更改默认城市id
   function SetDefaultCityID(CityIdValue,domain)
   
{    
        
var DefaultCityId = "UnionDefaultCityIdShow";
        setCookie('UnionDefaultCityIdShow',CityIdValue,'','',domain,'');
        
//setCookie('UnionDefaultCityIdShow',CityIdValue,'Fl','',domain,'');
   }
 
   
//更改默认城市名称
   function SetDefaultCityName(CityNameValue,domain)
   
{    
        
var DefaultCityId = "wwwUnion58ComDefaultCityName";
        setCookie('wwwUnion58ComDefaultCityName',CityNameValue,'','',domain,'');
   }
 
   
   
function setunionCookie(name, value, domain)
   
{
        setCookie(name,value,'','',domain,'');
   }

    
    
//**********************************************************************************************//
    
    
    
//获得某个cookies数值的函数
    function GetCookie(CookiesName)//参数是cookies的名字
    {
        
var aCookie = document.cookie.split("");//分数组    
        for (var i=0; i < aCookie.length; i++)
        
{
            
var aCrumb = aCookie[i].split("=");
            
if (CookiesName == aCrumb[0]) //检测名字
            {
                
return unescape(aCrumb[1]);//得到这个名字的cookies的数值
                break;        
            }

        }

        
return "";
    }

    
    
function WriteCookie(name, value)
    
{
        document.cookie 
= name + "=" + escape(value)+";expires=;path=;domain=webunion.58.com;secure=;";
        
/*
        document.cookie = name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : ""); 
*/

    }

    
    
function GetCookie_265(name) 
    

        
var arg = name + "="
        
var alen = arg.length; 
        
var clen = document.cookie.length; 
        
var i = 0
        
while (i < clen) 
        

            
var j = i + alen; 
            
if (document.cookie.substring(i, j) == arg) 
                
return getCookieVal_265 (j); 
            i 
= document.cookie.indexOf(" ", i) + 1
            
if (i == 0break
        }
 
        
return null;
    }

    
    
      
    
// utility function to retrieve an expiration data in proper format;
    function getExpDate(days, hours, minutes)
    
{
        
var expDate = new Date();
        
if(typeof(days) == "number" && typeof(hours) == "number" && typeof(hours) == "number")
        
{
            expDate.setDate(expDate.getDate() 
+ parseInt(days));
            expDate.setHours(expDate.getHours() 
+ parseInt(hours));
            expDate.setMinutes(expDate.getMinutes() 
+ parseInt(minutes));
            
return expDate.toGMTString();
        }

    }


    
//utility function called by getCookie()
    function getCookieVal(offset)
    
{
        
var endstr = document.cookie.indexOf(";", offset);
        
if(endstr == -1)
        
{
            endstr 
= document.cookie.length;
        }

        
return unescape(document.cookie.substring(offset, endstr));
    }


    
// primary function to retrieve cookie by name
    function getCookie(name)
    
{
        
var arg = name + "=";
        
var alen = arg.length;
        
var clen = document.cookie.length;
        
var i = 0;
        
while(i < clen)
        
{
            
var j = i + alen;
            
if (document.cookie.substring(i, j) == arg)
            
{
                
return getCookieVal(j);
            }

            i 
= document.cookie.indexOf(" ", i) + 1;
            
if(i == 0break;
        }

        
return;
    }


    
// store cookie value with optional details as needed
    function setCookie(name, value, expires, path, domain, secure)
    
{
        document.cookie 
= name + "=" + escape(value) +
            ((expires) 
? "; expires=" + expires : ""+
            ((path) 
? "; path=" + path : ""+
            ((domain) 
? "; domain=" + domain : ""+
            ((secure) 
? "; secure" : "");
    }


    
// remove the cookie by setting ancient expiration date
    function deleteCookie(name,path,domain) 
    
{
        
if(getCookie(name))
        
{
            document.cookie 
= name + "=" +
                ((path) 
? "; path=" + path : ""+
                ((domain) 
? "; domain=" + domain : ""+
                
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }

    }



posted on 2006-11-01 17:58  君子意如何  阅读(124)  评论(0编辑  收藏  举报