zoukankan      html  css  js  c++  java
  • ASP操作类似多维Cookies

    Cookies 并不支持多维或者数组,此例只是类似多维的操作。一般用于购物车之类的行为。如用cookies存储商品名称,ID,数量等

    示例:

    代码
     1 <!--#include file="ArrayCookies.asp"-->
     2 <%
     3 ' -------------------------
     4 ' 类似二维cookies示例
     5 ' V1.0 By EF
     6 ' -------------------------
     7 'Cookie_Clear "OrdereBooks"
     8 
     9 '写Cookies,可用数组或字符串
    10 Cookie_Add "OrdereBooks","商品1","1,2,3,1,2,3"
    11 Cookie_Add "OrdereBooks","商品2","1,2,3,1,,3"
    12 Cookie_Add "OrdereBooks","商品3",Array(1,2,4,6)
    13 
    14 '改Cookies
    15 Cookie_Add "OrdereBooks","商品1","1,2,3,1,2,4"
    16 
    17 '删Cookies
    18 Cookie_Remove "OrdereBooks","商品1"
    19 
    20 '
    21 Cookie_Read "OrdereBooks","商品1"
    22 
    23 '读Cookies长度(元素数量)
    24 Cookie_Length "OrdereBooks"
    25 
    26 '遍历字段名(结果用字符串),以下语句返回  商品2,商品3
    27 Cookie_Field_String "OrdereBooks"
    28 
    29 '遍历字段名(结果返回数组) 返回 Array("商品1","商品2")
    30 Cookie_Field "OrdereBooks"
    31 
    32 '读取整个Cookies的值
    33 Cookie_ReadAll "OrdereBooks"
    34 
    35 '清空Cookies
    36 Cookie_Clear "OrdereBooks"
    37 
    38 '读取某字段的第N个值,如1000001值的第4项,1
    39 SplitValue(Cookie_Read("OrdereBooks","商品1"),4)
    40 
    41 %>

    操作方法:

    注:并非用类来实现,其实封装成类也简单,不过我个人觉得多此一举。

    代码
      1 <%
      2 ' -------------------------
      3 ' 此文件用来处理类似二维的Cookies,用正则实现
      4 ' V1.0 By EF
      5 ' -------------------------
      6 
      7 ' -------------------------
      8 ' 作用 增加cookies值
      9 ' 参数 CookiesName,字段,值
     10 ' 说明 cookies中增加一个字段,值可以用数组或用,分隔的字串,也可是单个值
     11 ' -------------------------
     12 Function Cookie_Add(CookieName,FieldName,FieldValue)
     13     Dim Dictionary,TempCookiesStr,SplitCookiesStr,CookieField,CookieI
     14     TempCookiesStr = Request.Cookies(CookieName)
     15     IF TempCookiesStr <> "" THEN
     16         IF RegExpTest("$$" & TempCookiesStr & "$$","\$\$" & FieldName & "\|\|"THEN
     17             TempCookiesStr = ReplaceTest(TempCookiesStr,"\$\$" & FieldName & "\|\|" & "[^\$]+\$\$","$$$$" & FieldName & "||" & ArrayToString(FieldValue) & "$$$$")
     18         ELSE
     19             TempCookiesStr = TempCookiesStr & FieldName & "||" & ArrayToString(FieldValue) & "$$"
     20         END IF
     21     ELSE
     22         TempCookiesStr = "$$" & FieldName & "||" & ArrayToString(FieldValue) & "$$"
     23     END IF
     24     TempCookiesStr = Replace(TempCookiesStr,"$$$$","$$")
     25     Response.Cookies(CookieName) = TempCookiesStr
     26 End Function
     27 
     28 ' -------------------------
     29 ' 读取整个Cookies的值
     30 ' -------------------------
     31 Function Cookie_ReadAll(CookieName)
     32     Cookie_ReadAll = Request.Cookies(CookieName)
     33 End Function
     34 
     35 ' -------------------------
     36 ' 读取单个字段的值
     37 ' -------------------------
     38 Function Cookie_Read(CookieName,FieldName)
     39     On Error Resume Next
     40     Dim TempRetStr,TempCookiesStr
     41     TempCookiesStr = Request.Cookies(CookieName)
     42     If TempCookiesStr = "" or isnull(TempCookiesStr) then TempCookiesStr = ""
     43     TempRetStr = RegExpRet(TempCookiesStr,"\$\$" & FieldName & "\|\|" & "([^\$]+)\$\$",0)
     44     IF Err.Number <> 0 then
     45         Cookie_Read = ""
     46     Else
     47         Cookie_Read = TempRetStr
     48     End if
     49 End Function
     50 
     51 ' -------------------------
     52 ' 读取Cookies长度(字段数)
     53 ' -------------------------
     54 Function Cookie_Length(CookieName)
     55     Dim TempCookiesStr
     56     TempCookiesStr = Request.Cookies(CookieName)
     57     If InStr(TempCookiesStr,"||"= 0 then
     58         Cookie_Length = 0
     59     Else
     60         Cookie_Length = (len(TempCookiesStr) - len(replace(TempCookiesStr,"||","")))/2
     61     end if
     62 End Function
     63 
     64 ' -------------------------
     65 ' 遍历字段名,字符串
     66 ' -------------------------
     67 Function Cookie_Field_String(CookieName)
     68     Dim TempCookiesStr,TempSplit,Ti,TempRetStr
     69     TempCookiesStr = Request.Cookies(CookieName)
     70     TempSplit = split(TempCookiesStr,"$$")
     71     TempRetStr = ""
     72     For Ti = 0 to ubound(TempSplit)
     73         IF Instr(TempSplit(Ti),"||"<> 0 Then '合法ck
     74             TempRetStr =  TempRetStr & split(TempSplit(Ti),"||")(0& ","
     75         End IF
     76     Next
     77     if TempRetStr <> "" then
     78         TempRetStr = left(TempRetStr,len(TempRetStr)-1)
     79     end if
     80     Cookie_Field_String = TempRetStr 'split(TempRetStr,",")
     81 End Function
     82 
     83 ' -------------------------
     84 ' 遍历字段名,数组
     85 ' -------------------------
     86 Function Cookie_Field(CookieName)
     87     Cookie_Field = split(Cookie_Field_String(CookieName),",")
     88 End Function
     89 
     90 ' -------------------------
     91 ' 删除单个字段
     92 ' -------------------------
     93 Function Cookie_Remove(CookieName,FieldName)
     94     Dim Dictionary,TempCookiesStr,SplitCookiesStr,CookieField,CookieI
     95     TempCookiesStr = Request.Cookies(CookieName)
     96     IF TempCookiesStr <> "" THEN
     97         IF RegExpTest("$$" & TempCookiesStr & "$$","\$\$" & FieldName & "\|\|"THEN
     98             TempCookiesStr = ReplaceTest(TempCookiesStr,"\$\$" & FieldName & "\|\|" & "[^\$]+\$\$","$$$$")
     99         END IF
    100     END IF
    101     TempCookiesStr = Replace(TempCookiesStr,"$$$$","$$")
    102     Response.Cookies(CookieName) = TempCookiesStr
    103 End Function
    104 
    105 ' -------------------------
    106 ' 清除Cookies
    107 ' -------------------------
    108 Function Cookie_Clear(CookieName)
    109     Response.Cookies(CookieName) = ""
    110 End Function
    111 
    112 ' -------------------------
    113 ' 将数组转化成字符串
    114 ' -------------------------
    115 Function ArrayToString(ArrayStr)
    116     Dim TempArrayStr,SplitArrayStr,ArrayI
    117     IF isnull(ArrayStr) or ArrayStr = "" THEN
    118         ArrayToString = ""
    119         Exit Function
    120     END IF
    121     IF isArray(ArrayStr) THEN
    122         For ArrayI = 0 to Ubound(ArrayStr)
    123             TempArrayStr = TempArrayStr & ArrayStr(ArrayI) & ","
    124         Next
    125     ELSE
    126         SplitArrayStr = Split(ArrayStr,",")
    127         For ArrayI = 0 to Ubound(SplitArrayStr)
    128             TempArrayStr = TempArrayStr & SplitArrayStr(ArrayI) & ","
    129         Next
    130     END IF
    131     IF TempArrayStr <> "" then TempArrayStr = LEFT(TempArrayStr,LEN(TempArrayStr)-1)
    132     ArrayToString = TempArrayStr
    133 End Function
    134 
    135 ' ----------------------------------------------------------
    136 ' 正则匹配,匹配返回true,否则false
    137 ' ----------------------------------------------------------
    138 Function RegExpTest(strng, patrn)
    139   Dim regEx, retVal
    140   Set regEx = New RegExp 
    141   regEx.Pattern = patrn
    142   regEx.IgnoreCase = False '不区分大小写
    143   retVal = regEx.Test(strng)
    144   If retVal Then
    145     RegExpTest = true
    146   Else
    147     RegExpTest = false
    148   End If
    149 End Function
    150 
    151 ' ----------------------------------------------------------
    152 ' 正则替换
    153 ' ----------------------------------------------------------
    154 Function ReplaceTest(str1,patrn, replStr)
    155   Dim regEx               ' 建立变量。
    156   Set regEx = New RegExp               ' 建立正则表达式。
    157   regEx.Global = True
    158   regEx.Pattern = patrn               ' 设置模式。
    159   regEx.IgnoreCase = True               ' 设置是否区分大小写。
    160   ReplaceTest = regEx.Replace(str1, replStr)         ' 作替换。
    161 End Function
    162 
    163 ' ----------------------------------------------------------
    164 ' 正则查找,返回第一个匹配项的第N个结果
    165 ' ----------------------------------------------------------
    166 Function RegExpRet(strng,patrn,n)
    167   on error resume next
    168   Dim regEx, retVal ,RetStr,Matches ,Match
    169   Set regEx = New RegExp
    170   regEx.Pattern = patrn
    171   regEx.IgnoreCase = False
    172   set Matches  = regEx.Execute(strng)
    173   set Match = Matches(0)
    174   if err.number = 0 then
    175       RetStr = Match.SubMatches(n)
    176   else
    177       err.clear
    178       RetStr = ""
    179   end if
    180   RegExpRet = RetStr
    181 End Function
    182 
    183 ' -------------------------
    184 ' 读取字符串分隔的序列值
    185 ' 示例 Str = "1,2,,5,6"  SplitNum Str,2 == 2
    186 ' -------------------------
    187 Function SplitValue(byVal SplitStr,byVal num)
    188     Dim TempSplit,ReturnStr
    189     TempSplit = split(SplitStr,",")
    190     num = num - 1
    191     IF num > ubound(TempSplit) then
    192         SplitValue = ""
    193     Else
    194         IF TempSplit(num) <> "" and not isnull(TempSplit(num)) THEN
    195             SplitValue = TempSplit(num)
    196         ELSE
    197             SplitValue = ""
    198         END IF
    199     END IF
    200 End Function
    201 %>
  • 相关阅读:
    SR-IOV(Single Root I/O Virtualization)
    DHCP&DHCPv6
    Linux 上的基础网络设备详解
    当Linux用尽内存-oom
    真爱了--网络工程师技能图谱
    程序员必备技能:如何画好架构图?
    Linux内存使用情况以及内存泄露情况
    Neutron 消息回调系统
    linux bridge
    OpenStack-Neutron-code
  • 原文地址:https://www.cnblogs.com/gsyifan/p/1727327.html
Copyright © 2011-2022 走看看