Posts mit dem Label detect werden angezeigt. Alle Posts anzeigen
Posts mit dem Label detect werden angezeigt. Alle Posts anzeigen

Montag, 13. Oktober 2008

Better Cookie Detect

In some cases, you might really really want to know it the visiting browser has thier cookies on or off. One reason that I an think off is that you are using some ajax stuff on your the landing page that lets unregistered users edit a list or somthing.

This is a way of doing it.

1. Check to see if a cookie has been set for the session,
2. if not then check to see if this ip is in the middle of cookie detection
3. if not then redirect to a method and check again - if no cookies are on now, then be sure they are off.

Now you can either ask them to put them on to gain full functionallity of the application, or let the application blend out functions that cause UPDATE, PUT or DESTROY requests and instead show a flash error message that informs them of the reduced functionality.

This means that you will force a redirect on each request for browsers that have thier cookies off.

In the application.rb add the following:

before_filter :cookies_required, :except => [:check_for_cookies]

protected

def cookies_on?
..!request.cookies["_yourapp_session"].to_s.blank?
end

def cookies_required
..return unless request.cookies["_yourapp_session"].to_s.blank?
..if DetectCookie.find_and_destroy_or_create_by_remote_ip(request.remote_ip, request.request_uri) then return end
..session[:return_to] = request.request_uri
..redirect_to detect_cookies_path
end

>ruby script/generate model DetectCookie remote_ip:string request_uri:string
>rake db:migrate

Create a controller: dectect_cookies.rb
add the following:

class DetectCookiesController < ApplicationController
..before_filter :cookies_required, :except => :show

..def show
....if request.cookies["_hausursel_session"].to_s.blank? #change to your cookie session name
......detect_cookie=DetectCookie.find_by_remote_ip(request.remote_ip)
......detect_cookie ? redirect_to(detect_cookie.request_uri) : redirect_to(cookie_path)
....else
......redirect_back_or_default(:controller => 'apartments')
....end
..end
end


Add the following to the detect_cokkie.rb file in the models folder:

class DetectCookie < ActiveRecord::Base

..def DetectCookie.find_and_destroy_or_create_by_remote_ip(remote_ip, request_uri)
....if detect_cookie=DetectCookie.find_by_remote_ip(remote_ip)
......DetectCookie.destroy(detect_cookie.id)
......true
....else
......DetectCookie.create(:remote_ip=> remote_ip, :request_uri => request_uri)
......false
....end
..end

end

Samstag, 11. Oktober 2008

Simple Cookie Detect

All PUT, UPDATE and DESTROY methods require the session cookie with protect_from_forgery or the application will break if a browser has cookies turned off when one of these methods are used.

Unfortunately, when a browser calls the application for the first time, the session cookie wil be written, but it wont be read untill the next page request. This makes it impossible to know if cookies are off, or of in the browser so anyone visiting the application first time and lets say, posts a form will be disapointed.

This is a simple solution to the problem. All controllers will call a cookies_required method when PUT, UPDATE or DESTROY methods are used. The cookies_required method looks to see it if the session cookie is empty - if it is, it will catch the http-referrer and redirect to the method check_for_cookies - if the session cookie is still empty, then we can be certain that cookies are off in the browser and refrain the user from using the
PUT, UPDATE or DESTROY methods. Instead we show a message about cookies being off and the application having limited functionality untill they are turned on.

Add the following to application.rb

..before_filter :cookies_required, :except => [:show, :check_for_cookies]


..def check_for_cookies
....if request.cookies["_cookie_detect_session"].to_s.blank?
......render :text => 'You absolutly Need Cookies on to use this function'
....else
......redirect_back_or_default(:controller => 'home')
....end
..end

protected

..def cookies_required?
....return unless request.cookies["_cookie_detect_session"].to_s.blank?
....session[:return_to] = request.request_uri
....redirect_to(:controller => 'home',:action => 'check_for_cookies')
..end

..def redirect_back_or_default(default)
....session[:return_to] ? redirect_to(session[:return_to]) : redirect_to(default)
....session[:return_to] = nil
..end