Create a python login

Discussion in 'Python' started by txn, Oct 18, 2006.

  1. txn

    txn

    Joined:
    Oct 18, 2006
    Messages:
    1
    Hi folks,

    I was trying to create a simple python login, but it seems does not work for me, here is the code i have done:


    Code:
    #!/soft/python-2.4-bin/python
    print "Content-Type: text/HTML\n\n"
    
    import cgi
    #form = cgi.FieldStorage()
    
    def check_login():
            form = cgi.FieldStorage()
            user = form.getfirst("username")
            password = form.getfirst("password")
            pwd = { 'user':    'password',
                    'ta':      'ta',
            }
             
            if pwd.has_key(user):
                    if password==pwd[user]:
                            print"You are now logged in"
                    else:    
                            print "Invalid username or password\n"
                            form1()
            else:
                    form1()
    
    def form1():
       print """
            <html>
            <body>
            <FORM ACTION="assign5.cgi" METHOD="POST">
            <P><FONT SIZE="5" COLOR="#990000">Login</FONT><BR>
            
            <TABLE BORDER="0" WIDTH="271">
            <TR>
            <TD WIDTH="48%">
                    <P ALIGN="RIGHT">Username:
            </TD>
            <TD WIDTH="1%">&nbsp;</TD>
            <TD WIDTH="51%"><INPUT TYPE="TEXT" NAME="username" SIZE="12"></TD>
            </TR>
            <TR>
                    <TD WIDTH="48%">
                            <P ALIGN="RIGHT">Password:
                    </TD>
                    <TD WIDTH="1%">&nbsp;</TD>
    <TD WIDTH="51%"><INPUT TYPE="PASSWORD" NAME="password" SIZE="12"></TD>
                    <INPUT TYPE = hidden NAME = "action\" VALUE = "display">
            </TR>
            <TR>
                    <TD WIDTH="48%">&nbsp;</TD>
                    <TD WIDTH="1%">&nbsp;</TD>
                    <TD WIDTH="51%"><INPUT TYPE="SUBMIT" NAME="submit" VALUE="Login"><INPUT TYPE="RESET" NAME="Reset" VALUE="Clear"></TD>
            </TR>
    </TABLE>
    </FORM>"""
    
    check_login()
    print"</BODY></HTML>"
    
    It works and shows the log in page. Now i can log in with the user name and pass correct, but if i put wrong pass or username then it doesn't say "Invalid username or password". I really want it say that "invalid username or password" with the log in form again but i have tried but it never work for me. Anyone has any idea? thanks first.
    txn, Oct 18, 2006
    #1
    1. Advertising

  2. txn

    seunrau

    Joined:
    May 7, 2014
    Messages:
    1
    Solution

    Hi, sorry if you got this answer a bit late but I found out the problem.


    Instead of:

    else: print "Invalid username or password\n"



    try:

    elif password != pwd[user]
    print "Invalid..."






    Hope this works.
    seunrau, May 7, 2014
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William F. Robertson, Jr.
    Replies:
    0
    Views:
    429
    William F. Robertson, Jr.
    Jul 2, 2003
  2. Hermit Dave

    Forms Login Page Not Login Out

    Hermit Dave, Jan 12, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    454
    Hermit Dave
    Jan 13, 2004
  3. Nans
    Replies:
    2
    Views:
    20,072
    Martin Marinov
    Jun 17, 2004
  4. Sasquatch
    Replies:
    2
    Views:
    1,284
    Sasquatch
    Oct 3, 2006
  5. shivakrshn49@gmail.com
    Replies:
    4
    Views:
    424
    Dennis Lee Bieber
    Oct 5, 2012
Loading...

Share This Page