federico ramirez wrote:
> Hey, im working on a cgi script, just a simple news system.
> And im stucked with the login system.
> GOD! I hate being a noob!
> Well, this is the login page
>
> ################################
> #!/usr/bin/python
>
> import cgi, dbm, string, Cookie
> import config
> request = cgi.FieldStorage()
>
> def loginform():
> print '''<form action="admin.py" method="post">
> <table border="0">
> <tr>
> <td>Username</td>
> <td><input name="user" type="text" id="user" /></td>
> </tr>
> <tr>
> <td>Password</td>
> <td><input name="pass" type="text" id="pass" /></td>
> </tr>
> <tr>
> <td> </td>
> <td><input type="submit" name="Submit" value="Login" /></td>
> </tr>
> </table>
> </form>'''
>
> def main():
> C = Cookie.SimpleCookie()
> if(C.has_key("admin_user") and C.has_key("admin_pass")):
> admin = config.getadmin()
> if(C["admin_user"].value == admin[0] and C["admin_pass"].value
> == admin[1]):
> config.makepage("You are already logged!")
> else:
> config.makepage("Wrong cookies...")
> elif(request.has_key("Submit")):
> admin = config.getadmin()
> username = config.clean(request["user"].value)
> password = config.clean(request["pass"].value)
> if(username == admin[0] and password == admin[1]):
> C["admin_user"] = username
> C["admin_pass"] = password
> print C
> print "Content-Type: text/html\n\n"
> config.startpage()
> print "Bienvenido",username,'!'
> config.endpage ()
> else:
> print "Content-Type: text/html\n\n"
> config.startpage()
> print "Incorrect username and password combination"
> config.endpage ()
> else:
> print "Content-Type: text/html\n\n"
> config.startpage()
> loginform()
> config.endpage()
>
> main()
> ################################
>
> That seems to work..but i cant get the cookies, i tried making this to
> get them but i have a 500 error -.- i hate it so much
>
> #################################
> #!/usr/bin/python
> try:
> import Cookie
> print "Content-Type: text/html\n\n"
> C = Cookie.SimpleCookie()
> print C.["admin_user"].value
> except:
> cgi.print_exception()
> ###################################
>
> i tried that in different ways but i cant get it to work
You might be interested in this article and the associated logintools,
either as an example or to use directly:
http://www.voidspace.org.uk/python/articles/cgi_web_applications_two.shtml#who-are-you
http://www.voidspace.org.uk/python/cgi.shtml#login
According to the article you need to add
if os.environ.has_key('HTTP_COOKIE'):
thiscookie.load(os.environ['HTTP_COOKIE'])
to your main() before you try to access the cookie values.
Kent
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor