Subject: Help with login function please
Posted By: Kamraniac Post Date: 12/11/2006 2:10:38 PM
Hi guys

I've been using the excellent Beginning Dreamweaver MX book to help with a website. In the book it helps you do a neat thing with Dreamweaver's login behaviour. Using this ASP code, a user can login and be presented with information on the same page that wasn't there prior to logging in.

<%
    MM_authorizedUsers="Admin, Customer"
    If Session("MM_Username") <> "" Then
      If (false Or CStr(Session("MM_UserAuthorization"))="") Or _
         (InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization"))>=1) Then
%>
Hello! <%
          End if
         End if
         %>


I was wondering how to modify this so that the opposite can be done, so that the user can see some text, such as 'Login', and once the user has logged in the 'Login' text will disappear. Once the user has logged out, the 'Login' text will reappear on the page.

I hope I've clearly relayed my problem! I would appreciate any help with this issue. Thank you.

Reply By: woodyz Reply Date: 12/11/2006 2:25:57 PM
It is the same thing...  an "if" block can do something when the "if" condition is true, or do some other action when it is false.

In pseudo code:

If UserIsProperlyLogged On
  Show normal stuff
else
  Show login stuff
End if

Make sense? In the pseudo code the UserIsProperlyLogged represents your logic that checks if the user is logged on.



Woody Z
http://www.learntoprogramnow.com
Reply By: Kamraniac Reply Date: 12/11/2006 2:58:40 PM
Hi
Thanks for your response. I understand your point but I'm not sure how to apply it to my problem. I'm new to ASP and it's a tricky language to get to grips with.

Reply By: dparsons Reply Date: 12/11/2006 3:04:16 PM
woody's pseudo code is as "easy" as it gets; pseudo code is what programmers use to lay out their code in plain english so that they can get an idea of what their application needs to do.  

This code is not how I would do this but you can get the basic concept.

If Session("UserName") = "" then
Response.Write("Please Login <a href='./login.asp'>here</a>!")
else
Response.Write("Hello " & Session("UserName"))
End if

-------------------------
I will only tell you how to do it, not do it for you.  
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
Reply By: Imar Reply Date: 12/11/2006 4:19:50 PM
Hi Kamraniac,

Glad you like the book. I think the answers you got in this thread should get you started. In addition, you can also add a simple Else clause to the code you already have:

If Session("MM_Username") <> "" Then
  If (false Or CStr(Session("MM_UserAuthorization"))="") Or _
        (InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization"))>=1) Then
%> Hello! <%
  End if
Else
  Response.Write("You're not logged in")
End if

Basically, the Else fires when the opposite of the If is true....

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Bruch Nr. 1 g-moll Op. 26: Vorspiel  (Allegro moderato) by Mendelssohn & Bruch (Track 4 from the album: Violinkonzerte - Hoelscher[/b]) What's This?
Reply By: woodyz Reply Date: 12/11/2006 4:55:14 PM
quote:
Originally posted by Imar

Hi Kamraniac,

Glad you like the book. I think the answers you got in this thread should get you started. In additional, you can also add a simple Else clause to the code you already have:

If Session("MM_Username") <> "" Then
  If (false Or CStr(Session("MM_UserAuthorization"))="") Or _
        (InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization"))>=1) Then
%>[/b] Hello! <%
  End if
Else
  Response.Write("You're not logged in")
End if

Basically, the Else fires when the opposite of the If is true....

Hope this helps,

Imar

This is very close, but there are two conditions here, and the second condition can also result in a "not logged in" state.  I would suggest moving both conditions to a function that returns true if the user is okay to enter the site (authenticated and authorized) and false otherwise.

Woody Z
http://www.learntoprogramnow.com
Reply By: dparsons Reply Date: 12/11/2006 4:59:19 PM
You can presume, unless the developer goes to great lengths, that there are only 2 states for a user: Logged On and Not Logged On in which case Imar's example works fine.

-------------------------
I will only tell you how to do it, not do it for you.  
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
Reply By: Imar Reply Date: 12/11/2006 5:03:23 PM
quote:
This is very close, but there are two conditions here, and the second condition can also result in a "not logged in" state.
No, it can't.

This is Dreamweaver generated code which always looks a bit awkward. Basically, this is user and role checking in one block of code.

Session("MM_Username") holds the username while Session("MM_UserAuthorization") holds the user's role.

If the username is empty, the user is not logged on. However, even if you don't have any roles, you can still be logged in. So, the nested if is only for roles checking....:
If Session("MM_Username") <> "" Then
  If (false Or CStr(Session("MM_UserAuthorization"))="") Or _
        (InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization"))>=1) Then
%> Hello! <%
  Else
    Response.Write("Sorry, you're not an Admin or a Customer")
  End if
Else
  Response.Write("You're not logged in")
End if
Obviously, moving code to a function to abstract the complexity is a great idea, but not something that Dreamweaver offers out of the box....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Small Time Shot Away by Massive Attack (Track 7 from the album: 100th Window) What's This?
Reply By: Kamraniac Reply Date: 12/11/2006 5:15:28 PM
Thank you so much woodyz, dparsons and Imar! You've been a great help. :)


Go to topic 53489

Return to index page 96
Return to index page 95
Return to index page 94
Return to index page 93
Return to index page 92
Return to index page 91
Return to index page 90
Return to index page 89
Return to index page 88
Return to index page 87