Skip to content

Instantly share code, notes, and snippets.

@atsheehan
Created May 30, 2014 14:43
Show Gist options
  • Save atsheehan/de1f105174eb4c166262 to your computer and use it in GitHub Desktop.
Save atsheehan/de1f105174eb4c166262 to your computer and use it in GitHub Desktop.
Using session persistence with Sinatra.
require 'sinatra'
# Be sure to set an environment variable containing your secret key.
# This will be used to encrypt the cookies that are sent to the client.
#
# $ export SECRET_TOKEN=super_secret_key_goes_here
use Rack::Session::Cookie, secret: ENV['SECRET_TOKEN']
get '/' do
if session[:visit_count]
session[:visit_count] += 1
"You've visited this page #{session[:visit_count]} time(s)."
else
session[:visit_count] = 1
"This is your first time here, welcome!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment