Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7849721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:37:01+00:00 2026-06-02T18:37:01+00:00

I have a login python script in which i want to take the username

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:37:03+00:00Added an answer on June 2, 2026 at 6:37 pm

    i just read that the Set-Cookie header stuff is suppose to be before the line:

    print "Content_type: text/html\n\n"
    

    so, i fixed my code again and it’s working fine now. it goes back to previous script in case of wrong username / password and it proceeds to next script after validation
    Here is the final code:

    #! usr/local/bin/python
    import cgi;
    import cgitb;
    import Cookie;
    import os;
    import sqlite3;
    
    cgitb.enable()
    username= None
    form= cgi.FieldStorage()
    
    #open connection
    conn= sqlite3.connect("manager.db")
    cur= conn.cursor()
    
    pagehead= """
        <html>
            <head> Redirecting </head>
            <body>
    
                """
    pagefoot="""<form method= POST action="http://localhost:8000/cgi-bin/page1.py">
                <p> Validated! <input type="submit" value="Enter"/> </p>
                </form>
            </body>
        </html> """
    errorpagefoot= """
    <form action="http://localhost:8000/cgi-bin/index.py">
    <p> Error! <input type="submit" value="Go Back"/> </p>
    </form>
    </body>
    </html>"""
    
    
    userName= form.getvalue('usernameLogin')
    userPW= form.getvalue('passwordLogin')
    userPWDatabase = conn.execute("SELECT username,passwrd FROM login WHERE username=? and passwrd=?",[userName,userPW])
    cur.fetchone()
    for result in userPWDatabase:
        userDb= result[0]
        pwDb= result[1]
        if userDb == userName and pwDb == userPW:
            #Create Cookie
            C= Cookie.SimpleCookie()
            #take the value of usernameLogin into the variable username
            username= form.getvalue('usernameLogin')
            #Set-Cookie header with the usernameLogin key
            C['usernameLogin'] = username
            print C
        elif userDb != userName and pwDb != userPW:
            print errorpagefoot
    
    print "Content_type: text/html\n\n"
    print pagehead
    if username:
        print pagefoot
    else:
        print errorpagefoot
    

    @Alexis

    Thanks for your time and help Alexis 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Normally we have login page with username, password filed and signin button. Using the
Hi I want to create a WCF service that have login method, which is
I have an little script which logs users that login to my Pidgin/MSN account
I have a VB script which I want to run with python27, when I
I'm using python and I have to login at some site, which asks for
I have a Python script which processes a .txt file which contains report usage
on my web site I want to have login/registration form in modal window done
i am doing one small application , wheich have login functionality, in the user
I have a login form which appears at the top of all of my
I have a login window with a Sign In button which is set as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.