Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Member UNarmed's Avatar
    Join Date
    Sep 2008
    Posts
    90
    Member #
    17377
    Hey this question pertains to coldfusion MX7 and dreamweaver 8. I am working on a new website and i have a area where the users are able to log in if they have already created an account. My database contains a table called users in which all my user's data is stored. How do i, once someone has logged in draw the data for only that user out of the table and display it to the user. I am trying to do something similar to myspace and facebook in which one you log in your data is displayed to you shortly after. I am new to coldfusion but i know some of the basics and i have been trying to get this right but i have been unable to figure it out thus far. Perhaps i am aiming a bit high here but haha i am eager to learn as much as possible. Help would be greatly appreciated!

  2.  

  3. #2
    Senior Member Eddy Bones's Avatar
    Join Date
    Jan 2004
    Location
    Washington, USA
    Posts
    1,054
    Member #
    4651
    In it's simplest state, you have a form page, an action page, and a page that displays all the user info once they've logged in.

    The form page (maybe login.cfm) has the username and password fields that the user fills in. This page submits to the action page (perhaps login_action.cfm). Example:

    Code:
    <html>
    <head>
    <title>Login Form</title>
    </head>
    <body>
    <form action="login_action.cfm" method="post">
    Username: <input type="text" name="username" />
    Password: <input type="password" name="password" />
    <input type="submit" name="submit" />
    </form>
    </body>
    </html>
    Once the form is submitted, you query your users table to check if the username and password they entered match a record in the database. Be sure to set up your datasource in the Coldfusion Administration panel. Your data might be structured differently, but here's a general example of how you'd build your query (on login_action.cfm):
    Code:
    <cfquery name="checkUser" datasource="[datasource]">
        select userID from users 
        where username = <cfqueryparam value="#trim(form.username)#" cfsqltype="cf_sql_varchar">
        and password = <cfqueryparam value="#trim(form.password)#" cfsqltype="cf_sql_varchar">
    </cfquery>
    That should give you back a result set with any users that match the username/password combination. If there is a record that matches, you can set a session variable with the userID to indicate this and then send them to another page with their user info (member_area.cfm), otherwise you'll need to send them back to the form page:
    Code:
    <cfif checkUser.recordCount>
        <cfset session.userID = checkUser.userID>
        <cflocation url="member_area.cfm" addtoken="no">
    <cfelse>
        <cflocation url="login.cfm" addtoken="no">
    </cfif>
    Finally you just need to build the member page (member_area.cfm) to display their data. First you need to make sure that they're actually logged in (check the session variable you set before), then you query your users table to get their information, and finally you display it:
    Code:
    <cfif structKeyExists(session, "userID") and len(session.userID)>
        <cfquery name="getUserData" datasource="[datasource]">
            select firstName, lastName, emailAddress, [whatever other data fields]
            from users
            where userID = <cfqueryparam value="#session.userID#" cfsqltype="cf_sql_integer">
        </cfquery>
    
        <cfoutput query="getUserData">
            Hello #firstName# #lastName#.<br>
            The email address we have on file is: #emailAddress#<br>
            <a href="logout.cfm">Logout</a>
        </cfoutput>
    <cfelse>
        <!--- No session set --->
        <cflocation url="login.cfm" addtoken="no">
    </cfif>
    You can build a logout page (logout.cfm) that deletes the session variable and redirects to the login form:
    Code:
    <cfif structKeyExists(session, "userID")>
        <cfset structKeyDelete(session, "userID")>
    </cfif>
    <cflocation url="login.cfm" addtoken="no">
    This is a very rough, untested, and unrefined example that I think is a good basis. You'll have to modify it a lot, but it's the basic concept. If you have any questions or want clarification, please don't hesitate to ask. I'd be happy to set this up in working files that you can download if needed.

    Also, if you have an questions regarding the syntax I'm using, you can either ask here or else look it up in the CF7 LiveDocs: http://livedocs.adobe.com/coldfusion/7/

  4. #3
    Member UNarmed's Avatar
    Join Date
    Sep 2008
    Posts
    90
    Member #
    17377
    Thanks a lot man ... this is exactly what i was hoping for =]

  5. #4
    Senior Member Eddy Bones's Avatar
    Join Date
    Jan 2004
    Location
    Washington, USA
    Posts
    1,054
    Member #
    4651
    Here's a working set of files. They'll required that you have the database set up the right way, but I've added a SQL file so you can set up a test database. I was using MySQL, but if you're using something else then you may have to tweak it a bit. If you want to use the database that you mentioned you already have, you'll just have to modify the queries that grab user info.

    Please note once again that this is really rough code and about as basic as you get. If this were a medium to large scale application and I was going to invest a lot of time in it, I would design a framework or use a pre-made one such as MachII, FuseBox, etc. Coldfusion 8 allowed for unhindered use of Object-Oriented Programming (OOP) techniques via Coldfusion Components (CFCs), and I would generally use that. But if you're still learning Coldfusion from near the beginning then that stuff might be too lofty for the moment.

    (As an aside, if you don't already know OOP, it's not best learned through Coldfusion.)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:35 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com