Jump to content

Login Script Tutorial


Xenon Design

Recommended Posts

I have seen alot of requests, not just on W3 for a login script. I will go step by step in showing how you set up a login script after you have set up your database.Login Script<IMPORTANT>To start off with you need a database with all your user information in it including username and password in a table called tblusers. Also a form with the input fields 'user' and 'pass'. Making sure the pass field is set to password to make the text *'s</IMPORTANT>

<%'Checks for form dataIf Request.Form <>"" Then  'Set rs and connection to DB    Set conn = Server.CreateObject("ADODB.Connection")  Set rs = Server.CreateObject("ADODB.Recordset")    'Ill use a dsn named USERTEST to simulate a DSN connection to the database    dsn="dsn=USERTEST"  sql="SELECT * FROM tblusers WHERE username = '"&Request.Form("user")&"'"    'Executing connection    conn.Open dsn  rs.Open sql, conn  'If there is a username record    If Not rs.EOF Then      If rs.Fields.Item("password").Value = Request.Form("password") Then       'If the password is correct        Session("user") = rs.Fields.Item("username").Value        'Writes the user session        Response.Redirect("memberarea.asp")        'Redirects to a member area      Else        'If the password is incorrect        Response.Write("Password is incorrect")      End If  Else    'If no username record    Response.Write("Username does not exist")   End IfElse'If no form input'DISPLAY FORMEnd If%>

This is a basic little code that will write a user session called "user" which can be written by writing

<%=Session("user")%>

So basically the script of logging in a user is just following: If username exists then -> check password -> if password is correct then login -> redirect to member areaOr if password is wrongIf username exists then -> check password -> if password is incorrect -> tell userExtra FeaturesIf your really good at ASP you will be able to change and complex-ify the code to visually show the password or username is incorrect by changing the input box to red via CSS or log the users IP address when they login or make a remember username cookie.If you want another tutorial on an extra feature please PM me and I will put one together. Also if you want a tutorial on another topic dont hezitate to PM me.Having problems with the script? Dont know how to do something?If you are having troubles with my script just reply with your problem and I will try to respond quickly. Also if theres any spelling mistakes just let me know and ill fix it :)Xenon Design :)

Link to comment
Share on other sites

thank u for this useful article ,i work in a college web site ,so they need a database to store the student marks to be the student able to show there marks on the internet , so , i make the login script but the problem is there are many Specializations and every Specialization have a certain subject to studies ,how i can divide every Specializations students when they are enter the user name and the password ?thank u

Link to comment
Share on other sites

so , i make the login script but the problem is there are many Specializations and every Specialization have a certain subject to studies ,how i can divide every Specializations students when they are enter the user name and the password
Hmm....best to discuss this with me ln MSN...I will be on today (from the point I post this till about 12 hours later)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...