This login form is probably going to be used for many projects, so it’s in a post all by itself.
#!/usr/bin/python print "Content-type: text/html\n\n" print "<html>" print "<head>" print "<title>Login To Online Quiz</title>" print "</head>" print "<body>" #GLOBAL VARIABLES USERNAME = 'username' PASSWORD = 'password' DB = 'mysql-database' import MySQLdb import os #import requests #this script doesn't like this (doesn't fail, just blank screen) # Import modules for CGI handling import cgi, cgitb def init(): #read the query params params = os.environ.get('QUERY_STRING','nichts') #DEBUG: #print 'params= ' + str(params) #print '<br>' #print '<br>' res = params.split('=') #TESTS THAT THERE IS A QUERY STRING #display based on if params were sent if str(res) == "['']": #DEBUG: #print ('no query params') #print '<br>' print "<h2>Login</h2>" print "<form action = 'index.py?login=1' method = 'post'>" print "<table width='100%' border='0px' bgcolor=lightgreen>" print "<tr>" print "<td><strong>User Name:</strong></td>" print "<td><input type = 'text' name = 'uname' placeholder='enter username'></td>" print "</tr>" print "<tr>" print "<td><strong>Password:</strong></td>" print "<td><input type = 'text' name = 'pword' placeholder='enter password'></td>" print "</tr>" print "<tr>" print "<td><input type = 'submit' value = 'Login' /></td>" print "</tr>" print "</table>" print "</form>" else: #DEBUG: #print ('some params were passed - show them') #print '<br>' #searchParams is an array of type [['key','value'],['key','value']] searchParams = [i.split('=') for i in params.split('&')] #parse query string for key, value in searchParams: #print('<b>' + key + '</b>: ' + value + '<br>\n') if key == 'login': form = cgi.FieldStorage() uname = form.getvalue('uname') pword = form.getvalue('pword') #print ("login: " + uname + " " + pword + "<br>now call login<br><br>") login(uname,pword) def login(username,password): conn = MySQLdb.connect('localhost',USERNAME,PASSWORD,DB) cursor = conn.cursor() #Python will handle the escape string for apostrophes and other invalid SQL characters for you sql = "SELECT `u_login`, `u_password` FROM `tblUsers` WHERE u_login = %s AND u_password= %s " cursor.execute(sql, (username, password)) # Get the number of rows in the result set numrows = cursor.rowcount #DEBUG: #print('<br><br>numrows = ' + str(numrows) + '<br><br>' ) if numrows > 0: print '<font color=green>**************Login Successful**************</font><br><br>Please wait while we redirect you...' else: print '<font color=red>**************Login Failed**************</font><br><br>' print "<a href='index.py'>Try Again</a>" # Close the connection conn.close() #start here: init() print "</body>" print "</html>"
Click here to see it in action
BTW: The user name and password are
demo
123
Click here for the online quiz management main project link