<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: cookies code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 12 May 2008 04:32:09 GMT</pubDate>
    <description>DZone Snippets: cookies code</description>
    <item>
      <title>Persistent Rails cookie session</title>
      <link>http://snippets.dzone.com/posts/show/5173</link>
      <description>Session cookies, the Rails-2 kind, are transient because that's safer.  In some applications safety isn't important.  The following makes the session cookies persist for a year.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ApplicationController &lt; ActionController::Base&lt;br /&gt;  before_filter :update_session_expiration_date&lt;br /&gt;&lt;br /&gt;private&lt;br /&gt;  def update_session_expiration_date&lt;br /&gt;    unless ActionController::Base.session_options[:session_expires]&lt;br /&gt;      ActionController::Base.session_options[:session_expires] = 1.year.from_now&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 25 Feb 2008 12:23:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5173</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>Getting Started With WWW::Mechanize</title>
      <link>http://snippets.dzone.com/posts/show/5134</link>
      <description>This Ruby code uses WWW:mechanize to act like a web browser.  &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; require 'rubygems'&lt;br /&gt; require 'mechanize'&lt;br /&gt;&lt;br /&gt; agent = WWW::Mechanize.new&lt;br /&gt; page = agent.get('http://google.com/')&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Refer to the documentation at http://mechanize.rubyforge.org/mechanize/. Then gem install mechanize, and try running the code in an irb session.&lt;br /&gt;&lt;br /&gt;output (extract):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;=&gt; #&lt;WWW::Mechanize::Page&lt;br /&gt; {url #&lt;URI::HTTP:0xfdbbbb286 URL:http://www.google.com/&gt;}&lt;br /&gt; {meta}&lt;br /&gt; {title "Google"}&lt;br /&gt; {iframes}&lt;br /&gt; {frames}&lt;br /&gt; {links&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "Images"&lt;br /&gt;   "http://images.google.com/imghp?hl=en&amp;tab=wi"&gt;&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "Maps"&lt;br /&gt;   "http://maps.google.com/maps?hl=en&amp;tab=wl"&gt;&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "News"&lt;br /&gt;   "http://news.google.com/nwshp?hl=en&amp;tab=wn"&gt;&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "Shopping"&lt;br /&gt;   "http://www.google.com/prdhp?hl=en&amp;tab=wf"&gt;&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "Gmail"&lt;br /&gt;   "http://mail.google.com/mail/?hl=en&amp;tab=wm"&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 12 Feb 2008 17:53:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5134</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Integrating reinvigorate name tags into a PHP site</title>
      <link>http://snippets.dzone.com/posts/show/4208</link>
      <description>This is a script which will intergrate reinvigorate name tags into your PHP site using cookies&lt;br /&gt;Put this on any page you want to have visitors enter their name&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;FORM ACTION="index.php" METHOD="GET"&gt;&lt;br /&gt;&lt;p&gt;Name &lt;INPUT TYPE=TEXT NAME= "n" SIZE=20&gt;&lt;br&gt;&lt;br /&gt;&lt;INPUT TYPE=SUBMIT VALUE="Submit!"&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Put this on your index.php page at the top before anything. This will set a cookie that will last for 2 months&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$Name = $_GET['n'];&lt;br /&gt;?&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$inTwoMonths = 60 * 60 * 24 * 60 + time(); &lt;br /&gt;setcookie('name', $Name, $inTwoMonths); &lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Put this at the first line after the &lt;body&gt; tag. You need to change the USER-ID to your user ID that &lt;br /&gt;you can get from the regular reinvigorate code. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script type="text/javascript" src="http://include.reinvigorate.net/re_.js"&gt;&lt;/script&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;var re_name_tag = "&lt;?php echo $Name; ?&gt;";&lt;br /&gt;re_("USER-ID");&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 25 Jun 2007 17:07:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4208</guid>
      <author>robbiet480 (Robbie Trencheny)</author>
    </item>
    <item>
      <title>Javascript cookies</title>
      <link>http://snippets.dzone.com/posts/show/4153</link>
      <description>Learn how to use an object with methods to save, read and erase cookies. Using these methods you can manipulate &lt;a href="http://www.webtoolkit.info/javascript-cookies.html"&gt;cookies&lt;/a&gt; on your site.&lt;br /&gt;Demo can be found on this script homepage - &lt;a href="http://www.webtoolkit.info/"&gt;free code and tutorials website&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt;*&lt;br /&gt;*  Javascript cookies&lt;br /&gt;*  http://www.webtoolkit.info/&lt;br /&gt;*&lt;br /&gt;**/&lt;br /&gt;&lt;br /&gt;function CookieHandler() {&lt;br /&gt;&lt;br /&gt;	this.setCookie = function (name, value, seconds) {&lt;br /&gt;&lt;br /&gt;		if (typeof(seconds) != 'undefined') {&lt;br /&gt;			var date = new Date();&lt;br /&gt;			date.setTime(date.getTime() + (seconds*1000));&lt;br /&gt;			var expires = "; expires=" + date.toGMTString();&lt;br /&gt;		}&lt;br /&gt;		else {&lt;br /&gt;			var expires = "";&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		document.cookie = name+"="+value+expires+"; path=/";&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	this.getCookie = function (name) {&lt;br /&gt;&lt;br /&gt;		name = name + "=";&lt;br /&gt;		var carray = document.cookie.split(';');&lt;br /&gt;&lt;br /&gt;		for(var i=0;i &lt; carray.length;i++) {&lt;br /&gt;			var c = carray[i];&lt;br /&gt;			while (c.charAt(0)==' ') c = c.substring(1,c.length);&lt;br /&gt;			if (c.indexOf(name) == 0) return c.substring(name.length,c.length);&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		return null;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	this.deleteCookie = function (name) {&lt;br /&gt;		this.setCookie(name, "", -1);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 17 Jun 2007 17:31:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4153</guid>
      <author>justas (Justas)</author>
    </item>
    <item>
      <title>using wget to download content protected by referer and cookies </title>
      <link>http://snippets.dzone.com/posts/show/3937</link>
      <description>1. get base url and save its cookies in file&lt;br /&gt;2. get protected content using stored cookies&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; $ wget --cookies=on --keep-session-cookies --save-cookies=cookie.txt http://first_page&lt;br /&gt; $ wget --referer=http://first_page --cookies=on --load-cookies=cookie.txt --keep-session-cookies --save-cookies=cookie.txt http://second_page&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 30 Apr 2007 20:27:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3937</guid>
      <author>ameaba (Andrei Kuzmin)</author>
    </item>
    <item>
      <title>Custom HTTP/HTTPS GET/POST queries in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/788</link>
      <description>First, the scripts sends the GET query to read the website cookies (for session, etc.), and then it sends a POST query with the received cookies and custom POST parameters.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;require 'net/https'&lt;br /&gt;&lt;br /&gt;http = Net::HTTP.new('profil.wp.pl', 443)&lt;br /&gt;http.use_ssl = true&lt;br /&gt;path = '/login.html'&lt;br /&gt;&lt;br /&gt;# GET request -&gt; so the host can set his cookies&lt;br /&gt;resp, data = http.get(path, nil)&lt;br /&gt;cookie = resp.response['set-cookie']&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# POST request -&gt; logging in&lt;br /&gt;data = 'serwis=wp.pl&amp;url=profil.html&amp;tryLogin=1&amp;countTest=1&amp;logowaniessl=1&amp;login_username=blah&amp;login_password=blah'&lt;br /&gt;headers = {&lt;br /&gt;  'Cookie' =&gt; cookie,&lt;br /&gt;  'Referer' =&gt; 'http://profil.wp.pl/login.html',&lt;br /&gt;  'Content-Type' =&gt; 'application/x-www-form-urlencoded'&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;resp, data = http.post(path, data, headers)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# Output on the screen -&gt; we should get either a 302 redirect (after a successful login) or an error page&lt;br /&gt;puts 'Code = ' + resp.code&lt;br /&gt;puts 'Message = ' + resp.message&lt;br /&gt;resp.each {|key, val| puts key + ' = ' + val}&lt;br /&gt;puts data&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 06 Oct 2005 03:50:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/788</guid>
      <author>paulgoscicki (Paul Goscicki)</author>
    </item>
  </channel>
</rss>
