Simple Login in ASP using MS Access database

Discussion in 'ASP' started by shabbir, Aug 19, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Create a database in MSAccess and save database as Studentrecords.mdb. Now add a table to the database using the following format
    Code:
    StudentNO  Text
    FName      Text
    SurName    Text
    Password   Text
    Save the table as tblStudent.

    Now create a new file and name it login.asp
    Code:
    <html>
    
    <head>
    <title>Home</title>
    </head>
    <body bgcolor="#003366">
    <center>
    <%
    'StudentNo Of Logged in user
    Dim Username
    'Retrieving Username
    Username=Request.QueryString("username")
    'Invalid Username password
    if UserName <> "" then
    	Response.Write "<font color='#FFAA00'>"
    	Response.Write "<font face='Verdana' size=2>"
    	Response.Write"Invalid UserName Password. Please Re-login"
    	Response.Write "</font></font>"
    end if
    %>
      <form method="POST" action="verify.asp">
      <table border="1" cellspacing="1" bordercolor="#111111" id="AutoNumber1" height="82">
        <tr>
          <td height="23"><font color="#FFFFFF" face="Verdana" size="2">Student Number</font></td>
          <td width="148" height="23">
            <p align="center">
            <font face="Verdana" color="#FFFFFF">
            <input type="text" name="username" size="20" value=""><font size="2">
            </font></font>
          </td>
        </tr>
        <tr>
          <td height="22"><font color="#FFFFFF" face="Verdana" size="2">Password</font></td>
          <td width="148" height="22">
          <p align="center"><font color="#FFFFFF" face="Verdana"><input type="password" name="password" size="20"></font></td>
        </tr>
        <tr>
          <td width="210" colspan="2" height="22">
          <p align="center"><font face="Verdana"><font color="#FFFFFF">
          <input type="checkbox" name="rememberme" value="ON"></font><font size="2" color="#FFFFFF">Remember 
          Me</font></font></td>
        </tr>
        <tr>
          <td width="210" colspan="2" height="22">
          <p align="center"><font face="Verdana" color="#FFFFFF"><input type="submit" value="LogIn" name="B1"><font size="2">&nbsp;</font><input type="button" value="New User" name="B2" onClick = "location.href('signup.asp');"></font></td>
        </tr>
      </table>
      </form>
      </center>
    </div>
    </body>
    </html>
    
    Now create another file and name it verify.asp
    Code:
    <body bgcolor="#003366">
    <%
    
    'Connection String
    Dim Conn
    'Query to be executed
    Dim SQLQuery
    'Recordset
    Dim rs
    'StudentNo Of Logged in user
    Dim UserName
    'Password of User
    Dim Password
    
    'Getting information from submitted form
    UserName = request.form("username")
    Password = request.form("password")
    RememberMe = request.form("rememberme")
    
    'If not blank Username password submitted
    if UserName <> "" or Password <> "" then
    	'Creating connection Object
    	set Conn=server.createobject("ADODB.Connection")
    	'Creating Recordset Object
    	set rs = Server.CreateObject("ADODB.Recordset")
    	'Initialising Provider String
    	connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="
    	connStr = connStr + server.MapPath("Studentrecords.mdb")
    
    	'Opening Connection to Database
    	Conn.open  connStr
    	
    	'Query to be executed
    	SQLQuery = "select * from tblStudent where StudentNo = '"&UserName&"' AND Password = '"&Password&"'"
    
    	'Retrieving recordset by executing SQL
    	set rs=Conn.execute(SQLQuery)
    	'If no records retrieved
    	if rs.BOF and rs.EOF then
    		Response.Redirect "login.asp?username=" & UserName
    	else 
    		'If remember me selected
    		if RememberMe = "ON" then
    			'Writing cookies permanently
    			Response.Cookies("UserName")=UserName
    			Response.Cookies("Password")=Password
    			Response.Cookies("UserName").Expires = Now() + 365
    			Response.Cookies("Password").Expires = Now() + 365
    			Response.Redirect "welcome.asp"
    		else
    			'writing cookies temporarily
    			Response.Cookies("UserName")=UserName
    			Response.Cookies("Password")=Password
    			Response.Redirect "welcome.asp"
    		end if
    		'Closing all database connections
    		Conn.Close
    		rs.close   
    		set rs = nothing
    		set Conn = nothing
    	end if
    else
    	'Invalid User
    	Response.Redirect "login.asp?UserName=blank"
    end if
    %>
    
    Upload the all the 3 files viz login.asp, verify.asp and Studentrecords.mdb in the current folder and you are ready to go.
     
    mayurvishal and (deleted member) like this.
  2. archu

    archu New Member

    Joined:
    Mar 12, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hello

    if i want to login using userid or email then what will be the code??

    Thank u
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    StudentNO can behave as that. The concenptually article will explain how to go about it and it can always be extended for the custom fields in the database.
     
  4. archu

    archu New Member

    Joined:
    Mar 12, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Thanks for the reply. I think you din't get my point. I am asking for the code which help me to login using both username and email id . Ie is if i want i can login with a user name. Or else with a email id . The same way which is there in monster.com

    Thanks in advance.

    bye
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Still my answer remains the same but i will add something more.

    If one verification process fails you can go with the other but you can also test if the username contains some predefined valid email format and then go for email validation or the username one.
     
  6. kimnhat

    kimnhat New Member

    Joined:
    Mar 14, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I try to apply you code in Macromedia MX asp vbscript page and it not verify the username and password. Could you please let me know what I did wrong. I am new to this software and I try to create simple webpage with user name and password. please help
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Did you try creating a normal ASP page and see if it works for you.
     
  8. kimnhat

    kimnhat New Member

    Joined:
    Mar 14, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi Shabbir,

    It actualy work but it no validate the username and password. I can login either username and passowd blank or I just type some thinf in those 2 field. When I click login it link to verify page. Thanks,
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    There is a check for blank username and password and so see if it is executed correctly and if not it should display the message
    Code:
    if UserName <> "" or Password <> "" then
    .
    .
    .
    else
        'Invalid User
        Response.Redirect "login.asp?UserName=blank"
    end if
     
  10. kimnhat

    kimnhat New Member

    Joined:
    Mar 14, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi Shabbir,

    My bad. I were missing some code that why it not works. Now it works fine. Thanks alot.
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    My pleasure.
     
  12. archu

    archu New Member

    Joined:
    Mar 12, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi shabeer,

    I have a problem. I have designed a form which takes the details from the user and stores in a database. till storing i was not finding any problem. Now i am giving edit / update for the same form. I want to assign values to the each feild , which is already available in the database. I know how to assign for a text box. but i am finding difficulty in case of a radiobutton/ check box/ list. Pls help me know.

    Thanks in advance
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    For check box / radio button there is a value attribute which will be set if that is selected and you can determine which one is selected provided you have unique values for each and then you can update the database accordingly.
     
  14. hello

    hello New Member

    Joined:
    May 5, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi I am new here. I am making forum and there i want login page first before view yhe forum and i am making this project in asp.net with c# so can anyone tell me how can i do this and one thing mor i am working with master page so also explain how can i make this in master page

    Thanks in advance :)
     
  15. Pedes69

    Pedes69 New Member

    Joined:
    Dec 4, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Dear shabbir,

    Your code is working perfect, thank you for sharing this.

    But i have a question.

    I would like to direct diffent users to diffent pages.
    Like this:
    -> User A has to redirect to page welcome1.asp
    -> User B has to redirect to page welcome2.asp
    -> User C has to redirect to page welcome3.asp
    -> User D has to redirect to page welcome1.asp
    and so on...

    Can you tell me how to implement this in you code.

    Thank you very much
    Peter
     
  16. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Just redirect after validating. You need to check the type of user which is also fetched when validating the user and then use the ASP's redirect function.
     
  17. cybermarc

    cybermarc New Member

    Joined:
    Jan 4, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I want to keep my database in a separate folder but the script won't work.

    connStr = connStr + server.MapPath("/private/mrecords.mdb")

    anything wrong with this?
     
  18. mendir200

    mendir200 New Member

    Joined:
    Feb 13, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hey Shabbir, great code!!

    I implemented it to check a MS Access field for an email address. The problem is I set it up so that if a user accidentally types in the wrong address, they are re-directed to a page that allows them to re-enter the address again. If they re-enter the correct Email from that page, they get this message: “ADODB.Recordset error '800a0e78' Operation is not allowed when the object is closed”.


    I looked into the problem and I believe it is a syntax issue, ref: http://databases.aspfaq.com/database/what-should-my-connection-string-look-like.html

    Thanks so much!
     
  19. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Yes its a syntax issue and you need to have the Recordset open to execute the SQL.
     
  20. biztalk

    biztalk New Member

    Joined:
    Feb 25, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I would also like to do I would like to direct diffent users to diffent pages.
    Like this:
    -> User A has to redirect to page welcome1.asp
    -> User B has to redirect to page welcome2.asp
    -> User C has to redirect to page welcome3.asp
    -> User D has to redirect to page welcome1.asp
    and so on...

    Is it possbile for you to give the code for this?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice