I have a login python script in which i want to take the username and password and pass it to another python script which validates the username and password with the values in the database. If the user exists, it will create a cookie with the username value set in the cookie and redirect to the next page, otherwise, it will bring it back to the previous page.
this is my index.py code:
#!/usr/bin/python
import cgi;
import cgitb;
import sqlite3;
import os;
import Cookie;
import sys;
cgitb.enable()
form= cgi.FieldStorage()
username= None
userID = None
userPW= None
#Open connection
conn= sqlite3.connect("manager.db")
cur= conn.cursor()
def createdb():
###Create table login
conn.execute(""" CREATE TABLE login (userid INTEGER PRIMARY KEY ,
username TEXT, passwrd TEXT)""")
##
###Create table excursion
conn.execute(""" CREATE TABLE excursion (excurid INTEGER PRIMARY KEY,
location TEXT, excurDate TEXT, excurTime TEXT, user INTEGER, FOREIGN KEY(user) REFERENCES login(userid))""")
##
#Create table sighting
conn.execute(""" CREATE TABLE sighting (sightid INTEGER PRIMARY KEY,
species TEXT, observation TEXT, loginuser INTEGER, userexcursion INTEGER, FOREIGN KEY(loginuser, userexcursion) REFERENCES excursion (user, excurid))""")
##
#Insert username and password in login table
conn.execute("""INSERT INTO login (userid,username,passwrd) VALUES(NULL,'Diego','diego')""")
conn.commit()
#Insert dummy data in excursion table
conn.execute("""INSERT INTO excursion (excurid,location,excurDate,excurTime,user) VALUES(NULL,'Macquarie','04/01/2012','6:00pm',1)""")
conn.execute("""INSERT INTO excursion (excurid,location,excurDate,excurTime,user) VALUES(NULL,'Carlton','04/05/2012','7:00am',1)""")
conn.commit()
#Insert dummy data in sighting table
conn.execute("""INSERT INTO sighting (sightid,species,observation,loginuser,userexcursion) VALUES(NULL,'Duck','long beak',1,1)""")
conn.execute("""INSERT INTO sighting (sightid,species,observation,loginuser,userexcursion) VALUES(NULL,'Parrots','beautiful and colorful',1,2)""")
conn.commit()
conn.close()
if not os.path.exists("manager.db"):
createdb();
#define start of page
pagehead1= """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Home </title>
<link rel='stylesheet' type='text/css' href='/index.css/'/>
</head>
<body>
"""
pagehead2="""
<form method=POST action="http://localhost:8000/cgi-bin/validate.py">
<div id="container">
<div id="header">
<h1> Field Note Manager </h1>
<p class= "description"> Observe...Record...Save! </p>
</div>
<div id="wrapper">
<div id="content">
<p> Username: <input type="text" name="usernameLogin"/> </p>
<br/>
<p> Password: <input type="password" name="passwordLogin"/> </p>
<br/>
<p id="login"> <input type="submit" name="loginBtn" value="Login"/> </p>
</div>
</div>
<div id="footer">
<p> Copyright 42578647, 2012 </p>
</div>
</div>
"""
pagefoot= """ </form>
</body>
</html> """
print "Content_type: text/html\n\n"
print pagehead1
print pagehead2
print pagefoot
and this is my code for the validation of the username with the database:
#! usr/local/bin/python
import cgi;
import cgitb;
import Cookie;
import os;
import sqlite3;
#open connection
conn= sqlite3.connect("manager.db")
cur= conn.cursor()
username= None
form= cgi.FieldStorage()
UserPW= [form.getvalue('usernameLogin'), form.getvalue('passwordLogin')]
isValidate = validate(UserPW);
if isValidate == 1:
print "Content_type: text/html\n\n"
print """
<html>
<head> Redirecting </head>
<body>
<form method= POST action="http://localhost:8000/cgi-bin/page1.py">
<p> Validated! <input type="submit" value="Enter"/> </p>
</form>
</body>
</html> """
elif isValidate == 0:
print "Content_type: text/html\n\n"
print """
<html>
<head> Redirecting </head>
<body>
<form method=POST action= "http://localhost:8000/cgi-bin/index.py">
<p> Username or Password incorrect! <input type="submit" value="Go back"/> </p>
</form>
</body>
</html>
"""
def validate(UserPW):
sql= "SELECT * FROM login"
userPWDatabase=cur.execute(sql)
cur.fetchall()
for record in userPWDatabase:
if record == UserPW:
#Create cookie
C= Cookie.SimpleCookie()
#take the value of the index.py form variable username
username= form.getvalue('usernameLogin')
#set the cookie with the usernameLogin key
C['usernameLogin']= username
print C
return 1
else:
return 0
I don't know where is the problem