Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Persistent Rails cookie session

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.

class ApplicationController < ActionController::Base
  before_filter :update_session_expiration_date

private
  def update_session_expiration_date
    unless ActionController::Base.session_options[:session_expires]
      ActionController::Base.session_options[:session_expires] = 1.year.from_now
    end
  end
end

Getting Started With WWW::Mechanize

This Ruby code uses WWW:mechanize to act like a web browser.

 require 'rubygems'
 require 'mechanize'

 agent = WWW::Mechanize.new
 page = agent.get('http://google.com/')


Refer to the documentation at http://mechanize.rubyforge.org/mechanize/. Then gem install mechanize, and try running the code in an irb session.

output (extract):
=> #<WWW::Mechanize::Page
 {url #<URI::HTTP:0xfdbbbb286 URL:http://www.google.com/>}
 {meta}
 {title "Google"}
 {iframes}
 {frames}
 {links
  #<WWW::Mechanize::Page::Link
   "Images"
   "http://images.google.com/imghp?hl=en&tab=wi">
  #<WWW::Mechanize::Page::Link
   "Maps"
   "http://maps.google.com/maps?hl=en&tab=wl">
  #<WWW::Mechanize::Page::Link
   "News"
   "http://news.google.com/nwshp?hl=en&tab=wn">
  #<WWW::Mechanize::Page::Link
   "Shopping"
   "http://www.google.com/prdhp?hl=en&tab=wf">
  #<WWW::Mechanize::Page::Link
   "Gmail"
   "http://mail.google.com/mail/?hl=en&tab=wm">

Integrating reinvigorate name tags into a PHP site

This is a script which will intergrate reinvigorate name tags into your PHP site using cookies
Put this on any page you want to have visitors enter their name
<FORM ACTION="index.php" METHOD="GET">
<p>Name <INPUT TYPE=TEXT NAME= "n" SIZE=20><br>
<INPUT TYPE=SUBMIT VALUE="Submit!">

Put this on your index.php page at the top before anything. This will set a cookie that will last for 2 months
<?php
$Name = $_GET['n'];
?>
<?php
$inTwoMonths = 60 * 60 * 24 * 60 + time(); 
setcookie('name', $Name, $inTwoMonths); 
?>

Put this at the first line after the <body> tag. You need to change the USER-ID to your user ID that
you can get from the regular reinvigorate code.
<script type="text/javascript" src="http://include.reinvigorate.net/re_.js"></script>
<script type="text/javascript">
var re_name_tag = "<?php echo $Name; ?>";
re_("USER-ID");
</script>


Javascript cookies

Learn how to use an object with methods to save, read and erase cookies. Using these methods you can manipulate cookies on your site.
Demo can be found on this script homepage - free code and tutorials website.


/**
*
*  Javascript cookies
*  http://www.webtoolkit.info/
*
**/

function CookieHandler() {

	this.setCookie = function (name, value, seconds) {

		if (typeof(seconds) != 'undefined') {
			var date = new Date();
			date.setTime(date.getTime() + (seconds*1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else {
			var expires = "";
		}

		document.cookie = name+"="+value+expires+"; path=/";
	}

	this.getCookie = function (name) {

		name = name + "=";
		var carray = document.cookie.split(';');

		for(var i=0;i < carray.length;i++) {
			var c = carray[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
		}

		return null;
	}

	this.deleteCookie = function (name) {
		this.setCookie(name, "", -1);
	}

}

using wget to download content protected by referer and cookies

1. get base url and save its cookies in file
2. get protected content using stored cookies

 $ wget --cookies=on --keep-session-cookies --save-cookies=cookie.txt http://first_page
 $ wget --referer=http://first_page --cookies=on --load-cookies=cookie.txt --keep-session-cookies --save-cookies=cookie.txt http://second_page

Custom HTTP/HTTPS GET/POST queries in Ruby

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.

require 'net/http'
require 'net/https'

http = Net::HTTP.new('profil.wp.pl', 443)
http.use_ssl = true
path = '/login.html'

# GET request -> so the host can set his cookies
resp, data = http.get(path, nil)
cookie = resp.response['set-cookie']


# POST request -> logging in
data = 'serwis=wp.pl&url=profil.html&tryLogin=1&countTest=1&logowaniessl=1&login_username=blah&login_password=blah'
headers = {
  'Cookie' => cookie,
  'Referer' => 'http://profil.wp.pl/login.html',
  'Content-Type' => 'application/x-www-form-urlencoded'
}

resp, data = http.post(path, data, headers)


# Output on the screen -> we should get either a 302 redirect (after a successful login) or an error page
puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
puts data

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS