CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Create Custom Session Variables

This example shows how to add a custom session variable when a user is logged in. In this case, we will store the user's name in a session variable.

  1. Add an On Click event to the 'Login' button of a Login form.
  2. Within the event, add the code below:

Note: In addition to creating the session variable, you should also clear it when the user logs out.

ASP

Function Login_DoLogin_OnClick(Sender)
Dim Connection1
  If Login_DoLogin_OnClick = True Then
     Set Connection1 = New clsDBConnection1
     Connection1.Open
     Session("User_Name") = CCDLookUp("User_Name","Users","User_ID="& Connection1.ToSQL(CCGetUserID(),ccsInteger), Connection1)
     Connection1.Close
     Set Connection1 = Nothing
  End if

End Function

PHP

function Login_DoLogin_OnClick(& $sender) {

  if ($Login_DoLogin_OnClick == true) {
     $db = new clsDBConnection1();
     CCSetSession("User_Name", CCDLookUp("User_Name","Users","User_ID=".$db->ToSQL(CCGetUserID(),ccsInteger), $db) );
     $db->close();
  }

}

Perl

sub Login_DoLogin_OnClick() {

  if ($Login_DoLogin_OnClick == 1) {
    $DBConnection1 = clsDBConnection1->new();
    $User_Name = CCDLookUp("First_Name","Users","User_ID=".$db->ToSQL(CCGetUserID(),$ccsInteger), $DBConnection1);
    CCSetSession("User_Name", $User_Name);
    $DBConnection1->{sth} = undef;
    $DBConnection1->{dbh} = undef;
  }

}

ColdFusion

<!---DoLogin_OnClick --->

  <CFIF CCLoginUser>
    <CFModule Template="CCDLookUp.cfm" table="employees" field="emp_name" where="emp_id=#Session.UserID#" Connection="IntranetDB">
    <CFLOCK NAME="Session" TIMEOUT="30"  TYPE="Exclusive">
      <CFSET Session.User_Name=CCDLookUp>
    </CFLOCK>
  </CFIF>

VB.NET

'Button Button_DoLogin Event OnClick. Action Custom Code

Session("User_Name") = Settings.Connection1DataAccessObject.ExecuteScalar("SELECT User_Name FROM Users WHERE User_ID=" &
Settings.Connection1DataAccessObject.ToSql(DBUtility.UserId.ToString(), FieldType._Integer))

C#

//Button Button_DoLogin Event OnClick. Action Custom Code

Session["User_Name"] = Settings.Connection1DataAccessObject.ExecuteScalar("SELECT User_Name FROM Users WHERE User_ID=" +
Settings.Connection1DataAccessObject.ToSql(DBUtility.UserId.ToString(), FieldType.Integer));

Java

//DoLogin_OnClick

  if (Utils.getUserId(e.getPage()) != null ) { 
     String userId = DBTools.toSql(Utils.getUserId(e.getPage()), JDBCConnection.TEXT, "Connection1");
     SessionStorage.getInstance(e.getPage().getRequest()).setAttribute("User_Name", 
	 DBTools.dLookUp("User_Name","Users","User_ID="+userId,"Connection1"));
 }

See also:

On Click event


On-line, printable versions and updates