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
What is a proposition?
vor 15 Jahren
Keine Kommentare:
Kommentar veröffentlichen