Server side cookie problems

R

rodmc

Hi, I am trying to set a cookie on a client computer using the Cookie
module however all I get is the text being printed in the browser
window. Can anyone point me in the right direction so that the cookie
data is set without it appearing in the browser? A shortened version
of the code is below, in the full version there is also userID check,
this seems to work ok. I have removed that portion for the time being
as its the writing part that is the problem.

import sys, os, string, cgi, Cookie, urllib2
from types import ListType

cookie = Cookie.SimpleCookie()
cookieHdr = os.environ.get("HTTP_COOKIE", "")
cookie.load(cookieHdr)

def writetocookie(number):
#writes the ID of the database entry to a cookie
cookie["dataid"]=number
print "Content-Type: text/html"
print
print "Set-Cookie: dataid=",cookie["dataid"].value


writetocookie(1)

I presume this is not the correct way to write a cookie, the examples
I have found online don't seem to provide much more information.

Kind regards,

rod
 
G

Gabriel Genellina

Hi, I am trying to set a cookie on a client computer using the Cookie
module however all I get is the text being printed in the browser
window. Can anyone point me in the right direction so that the cookie

def writetocookie(number):
#writes the ID of the database entry to a cookie
cookie["dataid"]=number
print "Content-Type: text/html"
print
print "Set-Cookie: dataid=",cookie["dataid"].value


I presume this is not the correct way to write a cookie, the examples
I have found online don't seem to provide much more information.

I don't know either if this is the right way, but surely the Set-Cookie
header must appear *before* the blank line; that blank line separates the
headers from the response body.
 
R

rodmc

Hi, I am trying to set a cookie on a client computer using the Cookie
module however all I get is the text being printed in the browser
window. Can anyone point me in the right direction so that the cookie
def writetocookie(number):
#writes the ID of the database entry to a cookie
cookie["dataid"]=number
print "Content-Type: text/html"
print
print "Set-Cookie: dataid=",cookie["dataid"].value
I presume this is not the correct way to write a cookie, the examples
I have found online don't seem to provide much more information.

I don't know either if this is the right way, but surely the Set-Cookie
header must appear *before* the blank line; that blank line separates the
headers from the response body.

Thanks, that seemed to work.

rod
 
R

rodmc

En Wed, 06 Feb 2008 15:27:53 -0200, rodmc <[email protected]>
escribi�:
Hi, I am trying to set a cookie on a client computer using the Cookie
module however all I get is the text being printed in the browser
window. Can anyone point me in the right direction so that the cookie
def writetocookie(number):
#writes the ID of the database entry to a cookie
cookie["dataid"]=number
print "Content-Type: text/html"
print
print "Set-Cookie: dataid=",cookie["dataid"].value
I presume this is not the correct way to write a cookie, the examples
I have found online don't seem to provide much more information.
I don't know either if this is the right way, but surely the Set-Cookie
header must appear *before* the blank line; that blank line separates the
headers from the response body.

Thanks, that seemed to work.

rod

Also how do I find out if a cookie has expired? I have tried various
methods but to no avail?

Cheers,

rod
 
G

Gabriel Genellina

Also how do I find out if a cookie has expired? I have tried various
methods but to no avail?

The browser won't send the cookie in that case. The server cannot tell
whether the cookie expired or it never existed, AFAIK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,203
Latest member
KaliShumat

Latest Threads

Top