forked from ririgoei/CS160-Team3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
storeUser2.cgi
72 lines (59 loc) · 2.04 KB
/
storeUser2.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
import cgi
import sys
import os
import psycopg2
# import MySQLdb
from datetime import datetime
import bcrypt
# Check if user's password matches
def checkPassword(userpassword, confirmpassword):
if userpassword == confirmpassword:
return True
else:
return False
# Check if username already exist in database
def checkUsername(cursor, conn, username):
sql = "select username from user_profile where username=%s"
cursor.execute(sql, (username,))
name = cursor.fetchall()
if not name:
return True
else:
return False
# Store user information into database
def storeToDB(cursor, conn, username, userpassword, firstname, lastname):
hashedpassword = bcrypt.hashpw(userpassword, bcrypt.gensalt())
sql = "insert into user_profile values (%s,%s,%s,%s,%s,%s)"
cursor.execute(sql, (username,hashedpassword,firstname,lastname, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), str(cgi.escape(os.environ["REMOTE_ADDR"]))))
print "Register Successs"
print "<br >" + hashedpassword
print "<br >" + str(cgi.escape(os.environ["REMOTE_ADDR"]))
conn.commit()
cursor.close()
conn.close()
# Main function
def main():
print "Content-type:text/html\n\n"
try:
# conn = MySQLdb.connect('localhost', "root", "2800", "cs160")
conn = psycopg2.connect("dbname='cs160' user='postgres' host='localhost' password='2800'")
cursor = conn.cursor()
formData = cgi.FieldStorage()
firstname = formData.getvalue('firstname')
lastname = formData.getvalue('lastname')
username = formData.getvalue('username')
userpassword = formData.getvalue('password')
confirmpassword = formData.getvalue('confirmpsw')
if checkPassword(userpassword, confirmpassword) == True:
if checkUsername(cursor, conn, username) == True:
storeToDB(cursor, conn, username, userpassword, firstname, lastname)
else:
print "\n\nUsername already exist. Please choose another username"
else:
print "Password doesn't match"
sys.exit()
except:
print "error"
if __name__ == "__main__":
main()