Given below is the script that I have written for Login page. It does not throw any error on run but on browsing (the 'login.py' in inetmgr), I am unable to redirect/validate the screen.
#!c:\Python25\python.exe import cgi import cgitb; cgitb.enable() print "Content-type: text/html" print """ <HTML> <HEAD> <TITLE>Login Form</TITLE> </HEAD> <BODY BGCOLOR = white> <TABLE BORDER = 0> <TR> <TH>UserName:</TH> <TD><INPUT type = text name = "user"></TD> <TR> <TR> <TH>Password:</TH> <TD><INPUT type = password name = "psw"> </TD> </TR> </TABLE> <INPUT TYPE = hidden NAME = "action" VALUE ="display"> <INPUT TYPE = SUBMIT NAME = "login" VALUE = "Login" onsubmit = validate() > </BODY> </HTML>""" def validate(): form = cgi.FieldStorage() txt_usr = form.getfirst("user") txt_psw = form.getfirst("psw") b_login = form.getfirst ("login") if(txt_usr !="" and txt_psw !=""): try: db = MySQLdb.connect(host='<host_ip>', user='<username>' , passwd='<pasword>', db='<db_name>') curs = db.cursor() curs.execute("SELECT query.. = %s;", txt_usr) pswrd = curs.fetchone() for pwd in pswrd: if pwd == txt_psw: print "Content-type: text/html" print """ <HTML> Second page </HTML> """ else: print "invalid password" except: print "Username incorrect! Enter valid username" else: print "Enter username and password"
I know that we can redirect to next page by including following in the login HTML :
<FORM METHOD = POST ACTION = "\Python\Search.py">
but this again goes to next page without validation..
I checked out few sites, which talks about 'Sessions' and 'Cookies'. It looks greek and latin to me.
It would be great if anyone can tell me
1) the changes that are required in this script
2) In Python how can i validate a page on Submit button instead of <Form> (any onsubmit property?)
3) Do I require to use Sessions and cookies.