Cookies in eruby mod_ruby

Can someone explain how to set/delete cookies using mod_ruby (eruby)?
Or even better, point me to some documentation which will show me how.
I’ve spent hours trying to find docs and had to cave in.

db

···


Mar 6 Hindenburg explodes and burns upon landing at Lakehurst, NJ, 1939
Mar 6 Lantern Day, Bejing

Daniel Bretoi wrote:

Can someone explain how to set/delete cookies using mod_ruby (eruby)?
Or even better, point me to some documentation which will show me how.
I’ve spent hours trying to find docs and had to cave in.

My first Google hit on eruby and cookie:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/24049

The japanese doesn’t tell me much, but the code seems sane, so I hope
the bit above isn’t japanese for “why doesn’t this work”. :stuck_out_tongue:

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Hi,

Can someone explain how to set/delete cookies using mod_ruby (eruby)?
Or even better, point me to some documentation which will show me how.
I’ve spent hours trying to find docs and had to cave in.

If you’re using “cgi.rb” it’s like the following:

require “cgi”

cgi = CGI::new(“Html4Tr”)

cookie1 = CGI::cookie::new(“name”, “value1”, “value2”, …)
cgi.out({“cookie” => [cookie1]}){
“… body of you page”
}

						matz.
···

In message “cookies in eruby mod_ruby” on 03/03/06, Daniel Bretoi lists@debonair.net writes:

Personally, I don’t quite like the CGI#out style of doing things so I set
the headers myself then output the html my own way. It’s just as easy and
I feel like I’m in a bit more control. Here is a sample eruby program to
set a couple cookies:

<%
require ‘cgi’

get an expiration date string for a year from now

ed = CGI.rfc1123_date(Time.now + (60 * 60 * 24 * 365))

r = Apache.request
r.content_type = ‘text/html’
r.headers_out.add(‘Set-Cookie’, ‘name=frank; path=/; expires=%s’ % ed)
r.headers_out.add(‘Set-Cookie’, ‘language=eruby; path=/; expires=%s’ % ed)
r.send_http_header
%>

Here are the headers that came in:
<% r.headers_in.each { |h| p h } %>
Here are the headers we added and sent out:
<% r.headers_out.each { |h| p h } %>

Note that to use this with mod_ruby or plain cgi you’d just have to
remove the <% and %> markers and then add code to display the html that
eruby is displaying for you.

Let me know if you have any questions.

–frank

PS: By the way, since you asked, to delete a cookie you’d just set another
cookie with the same variable and path but with an expiration date in the
past.

···

On Thu, 06 Mar 2003 13:44:30 +0900, Daniel Bretoi wrote:

Can someone explain how to set/delete cookies using mod_ruby (eruby)?
Or even better, point me to some documentation which will show me how.
I’ve spent hours trying to find docs and had to cave in.

After I sent this out I thought I should clarify what I said for non
mod_ruby plain old cgi execution. Of course you won’t have the Apache
request hanging around so you’ll have to write your own headers. Replace
the entire ‘r’ block with something like:

ERuby.noheader = true
puts “Set-Cookie: name=frank; path=/; expires=%s” % ed
puts “Set-Cookie: language=eruby_cgi; path=/; expires=%s” % ed
puts “Content-type: text/html\n\n”

ERuby.noheader will tell eruby to not output headers on its own since
we’ll want to write them yourself. Later on in the script for the headers
display you will need to replace that portion too since, again, the Apache
object is not around in cgi land.

Either way, the original message should be just fine since you wanted
mod_ruby assistance in the first place. :wink:

–frank

···

On Thu, 06 Mar 2003 09:26:27 -0600, Frank Fejes wrote:

Personally, I don’t quite like the CGI#out style of doing things so I set
the headers myself then output the html my own way. It’s just as easy and
I feel like I’m in a bit more control. Here is a sample eruby program to
set a couple cookies:

<%
require ‘cgi’

get an expiration date string for a year from now

ed = CGI.rfc1123_date(Time.now + (60 * 60 * 24 * 365))

r = Apache.request
r.content_type = ‘text/html’
r.headers_out.add(‘Set-Cookie’, ‘name=frank; path=/; expires=%s’ % ed)
r.headers_out.add(‘Set-Cookie’, ‘language=eruby; path=/; expires=%s’ % ed)
r.send_http_header
%>

Here are the headers that came in:
<% r.headers_in.each { |h| p h } %>
Here are the headers we added and sent out:
<% r.headers_out.each { |h| p h } %>

Let me know if you have any questions.

Why don’t you like the .out method? I couldn’t even get it to work, and
I presumed that was because I was using eruby rather than in CGI format.
Does the .out actually force you to put the whole content within the
bracket?

db

···

On Fri, Mar 07, 2003 at 01:33:32AM +0900, Frank Fejes wrote:

–frank

PS: By the way, since you asked, to delete a cookie you’d just set another
cookie with the same variable and path but with an expiration date in the
past.


Mar 6 Hindenburg explodes and burns upon landing at Lakehurst, NJ, 1939
Mar 6 Lantern Day, Bejing

ERuby.noheader will tell eruby to not output headers on its own since
we’ll want to write them yourself. Later on in the script for the headers
display you will need to replace that portion too since, again, the Apache
object is not around in cgi land.

If I leave it with it’s own headers, will it be interfering with mine?

Either way, the original message should be just fine since you wanted
mod_ruby assistance in the first place. :wink:

Indeed :slight_smile: One more question, expiration and deletion?

db

···

On Fri, Mar 07, 2003 at 01:33:29AM +0900, Frank Fejes wrote:

That is my basic impression of it…perhaps someone who knows and loves it
can provide a better argument for it. All the examples using the CGI class
I’ve seen wrap tags and content within blocks. Personally, that’s just
not the way I work with content. Plus, with eruby, it doesn’t
make much sense since the code is embedded in the html, rather than the
other way around.

Out of curiosity, are you up and running with the code I posted?

–frank

···

On Fri, 07 Mar 2003 05:09:06 +0900, Daniel Bretoi wrote:

On Fri, Mar 07, 2003 at 01:33:32AM +0900, Frank Fejes wrote:

Let me know if you have any questions.

Why don’t you like the .out method? I couldn’t even get it to work, and
I presumed that was because I was using eruby rather than in CGI format.
Does the .out actually force you to put the whole content within the
bracket?

ERuby.noheader will tell eruby to not output headers on its own since
we’ll want to write them yourself. Later on in the script for the headers
display you will need to replace that portion too since, again, the Apache
object is not around in cgi land.

If I leave it with it’s own headers, will it be interfering with mine?

I believe it will, however check it for yourself. Either way, you don’t
need this new block since you’ll be running with mod_ruby and can use
the Apache request object.

Either way, the original message should be just fine since you wanted
mod_ruby assistance in the first place. :wink:

Indeed :slight_smile: One more question, expiration and deletion?

My first post explained how to delete:

to delete a cookie you’d just set another cookie with the same
variable and path but with an expiration date in the past.

For expiration, you need to use the rfc 1123 date format. I require ‘cgi’
since it includes the very useful CGI::rfc1123_date class method. It
converts a Time instance into the format that the cookie string uses.

ed = CGI.rfc1123_date(Time.now + (60 * 60 * 24 * 365))

That will return a date string of one year from now (60 seconds * 60
minutes * 24 hours * 365 days). If you want an expiration date in the
past, just do something like:

ed = CGI.rfc1123_date(Time.now - (60 * 60 * 24 * 365))

Have fun!

–frank

···

On Fri, 07 Mar 2003 05:17:20 +0900, Daniel Bretoi wrote:

On Fri, Mar 07, 2003 at 01:33:29AM +0900, Frank Fejes wrote:
From my first post:

That is my basic impression of it…perhaps someone who knows and loves it
can provide a better argument for it. All the examples using the CGI class
I’ve seen wrap tags and content within blocks. Personally, that’s just
not the way I work with content. Plus, with eruby, it doesn’t
make much sense since the code is embedded in the html, rather than the
other way around.

Out of curiosity, are you up and running with the code I posted?

I will as soon as I get a chance. My one week old baby boy is requireing
some time from me :stuck_out_tongue: This I think inturn explains why I asked things you
had already answered. Sorry about that. I will let you know.

db

···

On Fri, Mar 07, 2003 at 05:34:03AM +0900, Frank Fejes wrote:

–frank


Mar 6 Hindenburg explodes and burns upon landing at Lakehurst, NJ, 1939
Mar 6 Lantern Day, Bejing

Out of curiosity, are you up and running with the code I posted?

worked beautifully :slight_smile: thanks a bunch.

db

···

–frank


Mar 7 Aristotle died, 322BC
Mar 7 Sir John Frederick William Herschel born, 1792, astronomer
Mar 8 Alvan Clark born, 1804, astronomer & lens manufacturer
Mar 8 Howard Aiken born, 1900
Mar 9 “GOTO considered harmful” (E.J. Dijkstra) published in CACM, 1968
Mar 8 Deaths of Denethor & Theoden (LOTR)
Mar 7* Purim - Feast of Lots
Mar 8 First Annual International Women’s Day, 1909
Mar 8 International Women’s Day in U.S.S.R.
Mar 8 Syrian National Day in Libyan Arab Republic
Mar 8 Women’s Day in Guinea-Bissau, Taiwan and Yemen Democratic Republic
Mar 8 Youth Day in Zambia
Mar 9 Decoration Day in Liberia
Mar 9 Falgun Purnima Day in Nepal
Mar 7 Last Gilbert & Sullivan opera produced, 1896
Mar 8 Ron “Pigpen” McKernan (Grateful Dead) dies in California, 1973
Mar 9 Robin Trower is born in London, 1945
Mar 9* Parashat Pekudei
Mar 9* Parashat Va-Yakhel
Mar 9* Ha-Hodesh
Mar 9* Shabbat Mevarekhim