Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies not saving -- Cookie module depreciated? 1

Status
Not open for further replies.

khanza

Technical User
Feb 20, 2003
70
US
Here's the cookie code that isn't saving --

cookie = Cookie.Cookie()
cookie['username'] = u
cookie['url'] = url
cookie['username']['expires'] = 10000
cookie['url']['expires'] = 10000
cookie['username']['max-age'] = 10000
cookie['url']['max-age'] = 10000
cookie['username']['version'] = 1
cookie['url']['version'] = 1
cookie['username']['domain'] = "mysite.com"
cookie['url']['domain'] = "mysite.com"
cookie['username']['path'] = "/"
cookie['url']['path'] = "/"


I can do 'print cookie' after all the above code, but when i look in my cookies it isn't there... What's up?





Here's an excerpt from my error logs in apache (/usr/local/apache2/logs/error_log)
How can I fix this?
[Mon Oct 17 08:09:54 2005] [error] [client 164.116.x.x] /usr/local/lib/python2.4/Cookie.py:712: DeprecationWarning: Cookie/SmartCookie class is insecure; do not use it, referer: [Mon Oct 17 08:09:54 2005] [error] [client 164.116.x.x] DeprecationWarning), referer:

Thanks guys,
Khan
 
How are you causing the Cookie object to output headers to your client? Try posting the code you're actually running on the web server.
 
Alright, I edited my code, just to leave out my passwords, and my domains.

I realize this isn't the recommended way to do things, and I know (after I did the rest of the site like this) that there are far better ways to do it.

But this is the last piece, and I'd like to make this work.

Here's my code

( This wraps it, so this is the link to the pastebin )

Code:
#!/usr/bin/python
import cgi,os,MySQLdb,md5,sys,Cookie

form = cgi.FieldStorage()
try:
        u = form["username"].value
        u = MySQLdb.escape_string(u)
except KeyError:
        print '<font color="red"><b>Please include your username before submitting.</b></font>'
try:
        pp = form["password"].value
        p = md5.new(pp).hexdigest()
except KeyError:
        print '<font color="red"><b>Please include your password before submitting.</b></font>'
conn = MySQLdb.connect(host = "localhost", user = "user", passwd = "********", db = "test")
cursor = conn.cursor()
checkuserpass = "SELECT * from logincreds WHERE username='%s' AND password='%s'" % ( u, p )
checkadmin = "SELECT * from logincreds WHERE username='%s' AND admin='1'" % ( u )
if cursor.execute(checkadmin) == 1:
        print "Welcome Admin."
if cursor.execute(checkuserpass) == 1:
        cookie = Cookie.Cookie()
        cookie['username'] = u
        cookie['password'] = p
        cookie['username']['expires'] = 10000
        cookie['password']['expires'] = 10000
        cookie['username']['max-age'] = 10000
        cookie['password']['max-age'] = 10000
        cookie['username']['version'] = 1
        cookie['password']['version'] = 1
        cookie['username']['domain'] = "mydomain.com"
        cookie['password']['domain'] = "mydomain.com"
        cookie['username']['path'] = "/"
        cookie['password']['path'] = "/"
        print "Content-type: text/html\n\n"
        print cookie
        print '<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'[/URL]
        print '<html><head><link rel="stylesheet" type="text/css" href="[URL unfurl="true"]http://www.mydomain.com/folder/style.css"[/URL] /><title>Mytitle</title></head><body>'
        print '<table border="0"><tr><td><img src="[URL unfurl="true"]http://www.mydomain.com/folder/images/title_logo.gif"[/URL] /></td></tr></table><br><br>'
        print "<br />You are a member<br />"
else:
        print '<br /><font color="red"><b>Invalid Username or Password.</b></font><br />'
#numlines = cursor.fetchall()
print '</body></html>'
 
Code:
        print "Content-type: text/html\n\n"
        print cookie

Two newlines indicates then end of headers and the beginning of content. Print the cookie before sending two newlines.

 
Thank you _SO_ much.

I can't believe it was so simple, I really appreciate it!

:D

khan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top