Showing posts with label Cookie. Show all posts
Showing posts with label Cookie. Show all posts

Sunday, August 31, 2008

Mozilla Labs Ubiquity 试用

根据https://wiki.mozilla.org/Labs/Ubiquity 和 https://wiki.mozilla.org/Labs/Ubiquity/Ubiquity_0.1_Author_Tutorial
做了一个尝试,写了一个取cookie值的Ubiq:


CmdUtils.CreateCommand({
name: "cookie",
takes: {"cookie name": noun_arb_text},
icon: "http://www.mozilla.com/favicon.ico",
description: "Get named cookie value, or all cookie value.",
preview: function( pblock, cookieName ) {
var reg = new RegExp("(?:^|\\s+)" + cookieName.text + "=(.+?)(?:;|$)", "i");
var match = context.focusedWindow.document.cookie.match(reg);
if (match) pblock.innerHTML = match[1];
else pblock.innerHTML = context.focusedWindow.document.cookie;
},
execute: function( ) {
return false;
}
})

Saturday, August 23, 2008

Get Cookie value


function getCookie(cookie) {
var reg = new RegExp("(?:^|\\s+)" + cookie + "=(.+?)(?:;|$)", "i");
var match = document.cookie.match(reg);
// if (match) return RegExp.$1;
if (match) return match[1];
}

Thursday, July 24, 2008

利用window.name保存传递页面session状态

当在网页浏览中,所有的网页都是在同一个浏览器窗口(或者是tab)中浏览时,可以利用window.name进行页面状态的传递,相当于页面的session被持久在当前窗口的window.name中,不同的页面都可以读取到存在window.name中的值。
测试页地址:http://www.thomasfrank.se/sessvarsTestPage1.html
Reference:http://www.thomasfrank.se/sessionvars.html

Sunday, July 13, 2008

Cookie.js


var Cookie = {
set: function(name, value, daysToExpire) {
var expire = '';
var path = '; path=/';
if (daysToExpire != undefined) {
var d = new Date();
d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
expire = '; expires=' + d.toGMTString();
}
return (document.cookie = escape(name) + '=' + escape(value || '') + expire + path);
},
get: function(name) {
var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
return (cookie ? unescape(cookie[2]) : null);
},
erase: function(name) {
var cookie = Cookie.get(name) || true;
Cookie.set(name, '', -1);
return cookie;
},
accept: function() {
if (typeof navigator.cookieEnabled == 'boolean') {
return navigator.cookieEnabled;
}
Cookie.set('_test', '1');
return (Cookie.erase('_test') === '1');
}
};

reference: http://github.com/madrobby/scriptaculous/wikis

Wednesday, November 14, 2007

apache mod_usertrack 使用及其问题

为apache增加mod_usertrack module,只要动态生成一个so文件在配置文件里包括进来即可,之前有写了个文章关于这个操作过程。这次又用上了。

在虚拟主机的配置里再加上以下配置,其中具体的参数含义可参考此链接:http://httpd.apache.org/docs/1.3/mod/mod_usertrack.html


CookieTracking on
CookieStyle Cookie
CookieExpires "2 weeks"
CookieDomain .foo.bar
CustomLog logs/cookie-track.log "%{cookie}n %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
CookieName FOO_BAR

重启apache即可。
这个模块在Mozilla/Firefox中工作一切正常,而且可以做第3方的Cookie跟踪。
但是在IE(仅测试了IE6和IE7)中这个Cookie工作机制却类似于phpsessionid的工作机制,关闭IE重开之后却重新生了一个Cookie,如果不关则能正常记录用户的clickstream。但是从客户端查看HttpResponse则看得出来服务器端返回来的是正常的
请求,是让IE写个2周的Cookie,不过事实IE并不这样工作。至于用作第3方Cookie跟踪就更是不行,受P3P策略限制。

Wednesday, October 24, 2007

替代cookie在客户端实现数据持久性

在这个页面进行数据存储(仅限IE userData持久性)


<html>
<body>
<!-- This stylesheet defines a class named "persistent" -->
<style>.persistent { behavior:url(#default#userData);}</style>
<!-- This <div> element is a member of that class -->
<div id="memory" class="persistent"></div>

<script type="text/javascript">
var memory = document.getElementById("memory"); // Get persistent element
memory.setAttribute("username", 'user_name'); // Set data as attributes
memory.setAttribute("favoriteColor", '#ddd');

var now = (new Date( )).getTime( ); // now, in milliseconds
var expires = now + 10 * 24 * 60 * 60 * 1000; // 10 days from now in ms
memory.expires = (new Date(expires)).toUTCString( ); // convert to a string

memory.save("myPersistentData"); // Save the data

</script>
</body>
</html>

可以在这个页面读取,类似于操作cookie:

<html>
<body>
<!-- This stylesheet defines a class named "persistent" -->
<style>.persistent { behavior:url(#default#userData);}</style>
<!-- This <div> element is a member of that class -->
<div id="memory" class="persistent"></div>

<script type="text/javascript">
document.write("test result: <br/>");
var memory = document.getElementById("memory"); // Get persistent element
memory.load("myPersistentData"); // Retrieve saved data by name
var user = memory.getAttribute("username"); // Query attributes
var color = memory.getAttribute("favoriteColor");

document.write(user + '; ' + color);
</script>
</body>
</html>

Thursday, August 23, 2007

Ruby realize cookie management


#!/usr/bin/env ruby
require 'pp'
require 'erb'
require 'uri'
require 'hpricot'
require 'net/http'
require 'net/https'

module CookieMechanism
def cookies_hashed(set_cookie, path, domain)
expires = 'Mon, 01-Jan-2010 00:00:00 GMT'
arr_hashed = Array.new
if set_cookie
arr = set_cookie.split(/, /)
for i in 0 ... arr.size - 1
if arr[i] =~ /Expires=/ && arr[i + 1] =~ /.*GMT/
arr[i] << ', ' << arr[i + 1]
arr[i + 1] = ''
end
end
arr.delete_if {|del| del == ''}
arr.each do |cookie|
fields = cookie.split(';')
hashed = Hash.new
hashed['Path'] = path
hashed['Domain'] = domain
hashed['Secure'] = 0
fields.each do |f|
h = f.strip.split('=', 2)
h[0].capitalize! if h[0].capitalize =~ /Expires|Path|Domain|Secure/i
h[1] = 1 if h[0] == 'Secure'
hashed.merge!({h[0] => h[1]})
end
if hashed.has_key?('Expires')
hashed['Expires'] = 0 if Time.httpdate(hashed['Expires']) <= Time.now()
else
hashed['Expires'] = expires
end
arr_hashed << hashed
end
end
return arr_hashed
end

def cookies_process(arr_hashed_new, arr_hashed_old = Array.new)
arr_hashed_new.each do |new|
same_key = 0
new.each do |new_key, new_value|
if new_key !~ /Expires|Path|Domain|Secure/i
arr_hashed_old.each do |old|
old.each do |old_key, old_value|
if old_key !~ /Expires|Path|Domain|Secure/i && same_key == 0
if old_key == new_key
same_key = 1
end
end
break if same_key == 1
end
end
if same_key == 1
arr_hashed_old.each do |old|
old.each do |old_key, old_value|
if old_key == new_key
if new['Path'] == old['Path'] && new['Domain'] == old['Domain']
if new['Expires'] != 0
old[old_key] = new_value
old['Expires'] = new['Expires']
old['Secure'] = new['Secure']
else
arr_hashed_old.delete(old)
end
else
arr_hashed_old << new if new['Expires'] != 0
end
end
end
end
else
arr_hashed_old << new if new['Expires'] != 0
end
end
end
end
return arr_hashed_old
end

def cookies_request(arr_hashed_old, path, domain, secure = 0)
cookie = String.new
cookies = Array.new
arr_hashed_old.each do |old|
reg_str = Regexp.escape(old['Path'])
reg_path = Regexp.new('^' + reg_str)
reg_str = Regexp.escape(old['Domain'])
reg_domain = Regexp.new(reg_str + '$')
if path =~ reg_path && domain =~ reg_domain
if secure == 0
cookies << old if old['Secure'] == 0
else
cookies << old
end
end
end
cookies.sort!{|a, b| a['Path'] <=> b['Path']}.reverse!
cookies.each do |c|
c.each do |key, value|
if key !~ /Expires|Path|Domain|Secure/i
cookie == '' ? cookie += key + '=' + value : cookie += '; ' + key + '=' + value
end
end
end
return cookie
end

def add_cookie(response, path, domain, arr_hashed_old)
set_cookie = response['set-cookie'] # response: HTTPResponse object
arr_hashed_new = cookies_hashed(set_cookie, path, domain)
arr_hashed_old = cookies_process(arr_hashed_new, arr_hashed_old)
end

def get_http_headers(path, domain, arr_hashed_old)
headers = {
'Cookie' => cookies_request(arr_hashed_old, path, domain, 0),
'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
'Connection' => 'Keep-Alive',
'Accept-Language' => 'zh-cn'
}
end

def post_http_headers(path, domain, arr_hashed_old)
headers = {
'Cookie' => cookies_request(arr_hashed_old, path, domain, 0),
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
'Connection' => 'Keep-Alive',
'Accept-Language' => 'zh-cn'
}
end

def get_https_headers(path, domain, arr_hashed_old)
headers = {
'Cookie' => cookies_request(arr_hashed_old, path, domain, 1),
'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
'Connection' => 'Keep-Alive',
'Accept-Language' => 'zh-cn'
}
end

def post_https_headers(path, domain, arr_hashed_old)
headers = {
'Cookie' => cookies_request(arr_hashed_old, path, domain, 1),
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
'Connection' => 'Keep-Alive',
'Accept-Language' => 'zh-cn'
}
end
end