返回顶部

编辑

cookie

cookie小甜饼,是服务器在浏览器端保存的小段数据,在向服务器发起请求时会把cookie放到请求头中,发送到服务器端。

使用javascript可以直接显示页面上的cookie。

alert(document.cookie);

使用jquery的插件读写cookie的代码如下:

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

服务器端读写cookie

jsp读写cookie的代码

写入:

  <%
  Cookie cookie = new Cookie("write","cookie_write");
  cookie.setComment("这是一个Cookie写入测试!");
  cookie.setVersion(100);
  cookie.setMaxAge(3600);
  cookie.setDomain(".iisp.com");
  response.addCookie(cookie);
  %>

读取:

    <%
    Cookie cookies[]=request.getCookies();  
    out.println(" cookie的数量为:"+ cookies.length);
      out.println("<br>"+"<br>");
    for(int i = 0;i<cookies.length;i++){
      out.println("getName="+cookies[i].getName()+"<br>");  
      out.println("getValue="+cookies[i].getValue()+"<br>");  
      out.println("getComment="+cookies[i].getComment()+"<br>");  
      out.println("getDomain="+cookies[i].getDomain()+"<br>");
      out.println("getPath="+cookies[i].getPath()+"<br>");
      out.println("getMaxAge="+cookies[i].getMaxAge()+"<br>");
      out.println("getVersion="+cookies[i].getVersion()+"<br>");
      out.println("getSecure="+cookies[i].getSecure()+"<br>");
      out.println("<br>"+"<br>");
    }
    %> 
  1. **php代码** [代码片段(18行)] ...查看
  2. [代码片段(60行)] ...查看
  3. [代码片段(4行)] ...查看
  4. 2015-04-05 13:06:05COOKIE加密函数 by 男儿当自强
    示例用法:$eC = new encodeCookie;$e = $eC->encodeC ( md5 ('password') );$d = $eC->decodeC ( $e );echo "Original Cookie value : ".$d;echo "<br><br>";echo "Encoded Cookie value : ".$e; **取自www.54dev.com* ...查看
  5. [代码片段(63行)] ...查看
  6. **php代码** [代码片段(17行)] ...查看
  7. [代码片段(2行)] ...查看
  8. **login.html** [代码片段(39行)] **CookieBasedPasswordLogin.php** [代码片段(49行)] ...查看
  9. [代码片段(8行)] ...查看
  10. 2015-03-20 11:23:11cookie加密~ by 傅小黑
    蛮不错的 **[PHP]代码** [代码片段(74行)] ...查看
  11. [代码片段(12行)] ...查看
  12. 2015-09-24 19:08:17Flash Cookie Class by 灵剑子
    import flashCookie;var fc:flashCookie = new flashCookie;3种方法:setCookie - requires two argumnets, a name for your cookie and an object containing whatever data you want to store.readCookie - requires o ...查看
更多