Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I display their user name on every page until they log off?

Status
Not open for further replies.

leekikwan

Programmer
Nov 21, 2003
10
0
0
US
I have created a log in feature as follows. I now want to display username on top corner of every page they visit. Will you tell me how I do that?

Application.cfm
<!--- Create the application --->
<cfapplication name="MyApp" clientmanagement="Yes"
sessionmanagement="Yes"
sessiontimeout="#CreateTimeSpan(0,0,15,0)#"
applicationtimeout="#CreateTimeSpan(0,2,0,0)#">
<!--- Now define that this user is logged out by default --->
<CFPARAM NAME="session.allowin" DEFAULT="false">
<!--- Now if the variable "session.allowin" does not equal true, send user to the login page --->
<!---
the other thing you must check for is if the page calling this application.cfm is the "login.cfm" page
and the "Login_process.cfm" page since the Application.cfm is always called, if this is not checked
the application will simply Loop over and over. To check that, you do the following call
--->
<cfif session.allowin neq "true">
<cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">
<cfelseif ListLast(CGI.SCRIPT_NAME, "/") EQ "login_process.cfm">
<cfelse>
<!--- this user is not logged in, alert user and redirect to the login.cfm page --->
<script>
alert("You must login to access this area!");
self.location="login.cfm";
</script>
</cfif>
</cfif>

"login.cfm"
<form action="login_process.cfm" method="post">
Username: <input type="text" name="user_name" value=""><BR>
Password: <input type="password" name="user_pass" value=""><BR>
<input type="submit" name="login_user" value="Log In"><BR>
</form>
"login_process.cfm"
<!--- Get all records from the database that match this users credentials --->
<cfquery name="qVerify" datasource="userLogin">
SELECT user_name, user_pass
FROM tblAdmins
WHERE user_name = '#user_name#'
AND user_pass = '#user_pass#'
</cfquery>
<cfif qVerify.RecordCount>
<!--- This user has logged in correctly, change the value of the session.allowin value --->
<cfset session.allowin = "True">
<!--- Now welcome user and redirect to "members_only.cfm" --->
<script>
alert("Welcome user, you have been successfully logged in!");
self.location="/members_only.cfm";
</script>
< cfelse>
<!--- this user did not log in correctly, alert and redirect to the login page --->
<script>
alert("Your credentials could not be verified, please try again!!!");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
 
Code:
<!--- Create the application --->
<cfapplication name="MyApp" clientmanagement="Yes"
      sessionmanagement="Yes"
      sessiontimeout="#CreateTimeSpan(0,0,15,0)#"
      applicationtimeout="#CreateTimeSpan(0,2,0,0)#">
<!--- Now define that this user is logged out by default --->
<CFPARAM NAME="session.allowin" DEFAULT="false">
[b]<cfparam name="session.un" default="">

<cflock scope="session" timeout="20">
  <cfset suser=session.un>
  <cfset sAllow=session.AllowIn>
</cflock>[/b]

<!--- Now if the variable "session.allowin" does not equal true, send user to the login page --->
<!---
  the other thing you must check for is if the page calling this application.cfm is the "login.cfm" page 
  and the "Login_process.cfm" page since the Application.cfm is always called, if this is not checked 
  the application will simply Loop over and over. To check that, you do the following call 
--->
<cfif sAllow neq "true">
  [b]<cfif ListLast(CGI.SCRIPT_NAME, "/") is not "login.cfm" and ListLast(CGI.SCRIPT_NAME, "/") is not "login_process.cfm">[/b]
  <!--- this user is not logged in, alert user and redirect to the login.cfm page --->
  <script>
    alert("You must login to access this area!");
    self.location="login.cfm";
  </script>
  </cfif>
</cfif>

 "login.cfm" 
<form action="login_process.cfm" method="post">
   Username: <input type="text" name="user_name" value=""><BR>
   Password: <input type="password" name="user_pass" value=""><BR>
   <input type="submit" name="login_user" value="Log In"><BR>
</form>

"login_process.cfm"
<!--- Get all records from the database that match this users credentials --->
<cfquery name="qVerify" datasource="userLogin">
  SELECT     user_name, user_pass
  FROM    tblAdmins
  WHERE    user_name = '#user_name#'
       AND user_pass = '#user_pass#'
</cfquery>
<cfif qVerify.RecordCount>
  <!--- This user has logged in correctly, change the value of the session.allowin value --->
  [b]<cflock timeout="20" scope="session">[/b]
  <cfset session.allowin = "True">
  [b]<cfset session.un=qVerify.user_name>[/b]
  [b]</cflock>[/b]
  <!--- Now welcome user and redirect to "members_only.cfm" --->
  <script>
   alert("Welcome user, you have been successfully logged in!");
   self.location="/members_only.cfm";
  </script>
<cfelse>
  <!--- this user did not log in correctly, alert and redirect to the login page --->
  <script>
  alert("Your credentials could not be verified, please try again!!!");
  self.location="Javascript:history.go(-1)";
  </script>
</cfif>

Changes:

1. I created the param session.un and set suser to that value..
2. I modified your CFIF for verification to work faster.
3. I added a spot in login to set session.un so you can "carry the username".
4. I cflock'd references to sessions and made your session.un variable local as suser so that you don't have to put the cflock in when calling the variable.
5. Did the same localization for session.allowIn

logout and login and call #Suser# on one of your pages and you should see your username.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
i know ALFI wil have got a much better solution than i do but for simplicity i used a
<cfset session.user_id=qverify.user_id>

Then make a file called onrequestend.cfm
put it in the same directory as application
then have it so that it says
<cfquery name=user" datasource="DSN">
Select user_name
From tblAdmins
Where user_id=#session.user_id#
</cfquery>

Then have the file say
<cfoutput> Welcome, #user.user_name# </cfoutput>
This is an alternate one that i have thought of i would use webmidgits one he is far superior to me in CFM. I just thought that this would b a simple way to do it.

Hope this helps,

The way web design should be
 
Haha...

Actually no, yours is better for a few reasons.. (And I, personally, use a method somewhat like that)..

1. It provides some level of authentication
2. It allows you access to every detail.

The only flaw is that you wouldn't put that query in onrequestend.cfm, you'd put that in application.cfm.. In onrequestend.cfm, the query would run after all your content making it so you couldn't access the variable.

But here's what I do when I get into serious authentication.. I store the username and encrypted password in a session variable.. then I can kick them out while they're on the site just by editing their password if need be, because their query's where statement tries to match the password.

I also use an active field.. all my login queries say something like "where active=1" so if I deactivate the person, they can't touch another member page.

Thanks very much for the compliments, yours is a good method, as is mine, but this person didn't seem too bent on authentication.. just personalization.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
AFII, seeing as you are active in this thread i dont really need to start a new thread this is building on authentication that you and i discussed in the previous 2 posts.
You mentioned using an active=1
Would i use this in login_process.cfm
i.e.
<cfquery name="qVerify" datasource="userLogin">
SELECT user_name, user_pass
FROM tblAdmins
WHERE user_name = '#user_name#'
AND user_pass = '#user_pass#'
AND active = '#1#'
</cfquery>

or would i put a CFIF in it like

Code:
<cfset session.active = #qverify.active#>

<cfif session.active LT '0'> 
        alert("Disabled Account");
<cfabort> 
</cfif> 
Rest of login_process.cfm

Otherwise could you show me a way of implementing this as i want to be able to ban users

Hope this helps,

The way web design should be
 
You would use it in a login process..

And you mentioned running a query in your pages..

Code:
<cfquery name=user" datasource="DSN">
Select user_name
From tblAdmins
Where user_id=#session.user_id#
</cfquery>

you would use it there too..

Using it in the login process allows you to control which of your users can login.. Using it in your "every page" query allows you to kick someone out while they're using the site just by changing the value of active from 1 to.. for instance.. 0.. Since your query requires active=1, then active equalling 0 would kick the user.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top