473,614 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem dealing with cookies

Hello,

I have the following code:

#!/usr/local/bin/python
import cgi
import Cookie

C = Cookie.SimpleCo okie()
C["moo"] = "f"
C["moo"]["path"] = "/"
C["moo"]["expires"] = 60*60*60*60

#Print the headers
print C
print "Content-Type: text/html\n"

#Print starting html
print "<html><head><t itle>Cookie</title></head><body>"

form = cgi.FieldStorag e()

if C['moo'].value != "f":
print "I remember you %s" % (C["moo"].value)

elif form.has_key("n ame") and C["moo"].value == "f":
print "Thank you %s!" % (form["name"].value)
C["moo"] = form['name'].value
else:
print '<form method="POST" action="ses.py" >'
print '<input type="text" name="name">'
print '</form>'

print "</body></html>"

I am new in learning Python and I can't get this to work. Basically,
it asks you to input something and thanks you with the display of what
you entered.

It should remember you. SO when you refresh, it should say that it
remembers you. But it doesn't and shows the input box again.

What could possibly be wrong here?
Thanks in advance!

Fazer
Jul 18 '05 #1
1 2218
fa****@jaredweb .com (Fazer) writes:
I have the following code: [...] C = Cookie.SimpleCo okie()
C["moo"] = "f"
C["moo"]["path"] = "/"
C["moo"]["expires"] = 60*60*60*60

#Print the headers
print C
Here (the print statement), you're setting the client's cookie "moo"
to "f", no matter what. What you wanted was to put the form's data in
the cookie, if the request contains form data.

print "Content-Type: text/html\n"

#Print starting html
print "<html><head><t itle>Cookie</title></head><body>"

form = cgi.FieldStorag e()
[...]
if C['moo'].value != "f":
print "I remember you %s" % (C["moo"].value)
You're expecting C to have magically aquired a new value between the
point where you set it above and this point. How is that supposed to
happen?

elif form.has_key("n ame") and C["moo"].value == "f":
print "Thank you %s!" % (form["name"].value)
C["moo"] = form['name'].value

[...]

More of the same. You're setting C["moo"], but how is the client
supposed to find out? SimpleCookie isn't quite that magical. :-)

Remember the way all this gets executed: a request comes in, the web
server picks the right CGI script based on the request URL, and your
script sees the HTTP headers (including Cookie) as environment
variables. You use the headers from the environment (with something
like C.load(os.envir on["HTTP_COOKI E"]) and forms = cgi.FieldStorag e()
-- both look in os.environ, one explicitly, the other not), then you
output some HTTP headers and then some data (usually HTML). Request,
response. You're trying to sneak several request-response cycles into
one CGI execution.

So, you either want a single CGI script and some kind of switch (am I
looking at the first request, before the user has seen the form, or
the second, with the submitted form data?), or two CGI scripts (one
for the first request, one for the second with the form data -- set
the form's action HTML-attribute to point to that second CGI script).

Probably, you'll quickly find that life is simpler (for anything
non-trivial) if you use a web programming framework. Don't be afraid
of them! Albatross might be a good choice -- designed to be a small
step up from CGI, and pure Python so easy to install on a server --
but there are plenty of others.

In all cases, remember that you have to assume you're dealing not with
a well-behaved user (no such beast) using a well-behaved browser, but
an evil hacker crafting evil HTTP headers and data exactly how he
wants them.
John
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3401
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not establish secure channel for SSL/TLS" private void mainHttpCalls(string postData) { HttpWebRequest objRequest1 ; HttpWebRequest objRequest2 ;
10
2555
by: Tony Archer | last post by:
Gentlemen, your urgent help is needed in solving a STRANGE problem. I have two servers a dev box, and a production box. On the dev box everything works fine, in production it hoses: One of the things a user can do is click a "Change region" link which takes them to a map of the world. They can then select one of four regions. When they select a region it takes them to a page which reads a variable and then sets a cookie which is...
1
2225
by: Tony Archer | last post by:
(Forgive the dupicate post, I had left out the OS Version out of the prior post.) Gentlemen, your urgent help is needed in solving a STRANGE problem. I have two servers a dev box, and a production box. On the dev box everything works fine, in production it hoses: One of the things a user can do is click a "Change region" link which takes them to a map of the world. They can then select one of four regions.
0
1322
by: Ori | last post by:
Hi, In continue to another post which I have in this news group, I was able to find out some more details about the problem What is my problem? I currently build some kind of web-service which illustrates the web-site login, some query and retrieve some data from this websites.
2
1595
by: Netanel | last post by:
Hi, I have a site that I developed in ASP / VBScript. Some of the visitors are complaining that they can't get in into the system (using the login form of-course). My login form includes e-mail and passwords fields, that I check within the e-mail and passwords fields written in the database. Then, if all is OK and the info the visitor enter is valid, I write it into a cookie that next time the user get into my site, the system will...
1
1715
by: rushik | last post by:
Dear all, We have created a business portal for our organization. The technology used for that is LAMP. Our major access management system of the portal is based on cookies. we set some user level cookies at the time of login; using these cookies we provide access of our pages to user. The problem is, sometimes after login process we are getting fatal
0
1705
by: Claudio | last post by:
I have a demo app where cookies does not work. The first page create a cookies. The second read the contet. If I browse the pages via IE6.0 the pages does not work. If I browse the pages via FireFox the pages work fine. ************ SetCookies.asp <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
0
1352
by: jegray | last post by:
I am very much a beginner in dealing with connection statments. I am getting the following error: Microsoft OLE DB Provider for ODBC Drivers error '80004005' Data source name not found and no default driver specified /default.asp, line 32 Here is my connection include:
7
10200
by: cmrchs | last post by:
Hi, In Windows Vista: where does asp.net write its cookies? I use HttpCookie objCookie = new HttpCookie("nameCookie"); in Win2000 (and later) they used to be in C:\Documents and Settings\Administrator\Cookies but not anymore
0
8120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8423
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5537
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4048
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2560
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1705
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1420
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.