Wikis / Unreal Wiki / Legacy:Sessions

Here's an example on how to implement sessions into your application.

Sessions are usefull to keep a record of variables per user/session. Sessions are often used in web pages to keep track of temporary user settings when they visit your site.

Sessions class

This class manages all sessions

class Sessions extends Object;
 
var array<Session> sessions;
 
// create a new session
function Session createSession()
{
  sessions.length = sessions.length+1;
  sessions[sessions.length-1] = new(None) class'Session';
  sessions[sessions.length-1].Hash = createSessionHash();
  return sessions[sessions.length-1];
}
 
// get a session by it's hash
function Session getSession(string hash, optional bool bCreateNew)
{
  local int i;
  for (i = 0; i < sessions.length; i++)
  {
    if (sessions[i].Hash == hash) return sessions[i];
  }
  if (bCreateNew) return createSession();
  return none;
}
 
// destroy an existing session
function bool destroySession(Session session)
{
  local int i;
  for (i = 0; i < sessions.length; i++)
  {
    if (sessions[i].Hash == session.Hash) 
    {
      sessions.Remove(i, 1);
      return true;
    }
  }
  return false;
}
 
// create a unique session hash
function private string createSessionHash()
{
  local int i;
  local string temphash;
 
  do {
    temphash = Right("00000"$string(Rand(65536)), 5)$Right("00000"$string(Rand(65536)), 5);
    for (i = 0; i < sessions.length; i++)
    {
      if (sessions[i].hash == temphash) break;
    }
  } until (i == sessions.length);
  return temphash;
}

Session class

This class will contain the data for a single session.

class Session extends Object;
 
struct export KeyValuePair
{
    var string Key;
    var string Value;
};
 
// contains the unique identifier
var string hash;
// contains the data for this session
var array<KeyValuePair> Data;
 
// get the value of a var name, return sdefault if not found
// bFound is 1 when the value exists, 0 otherwise
function string getValue(string name, optional string sdefault, optional out int bFound)
{
  local int i;
  bFound = true;
  for (i = 0; i<data.length; i++)
  {
    if (data[i].key == name) return data[i].value;
  }
  bFound = false;
  return sdefault;
}
 
// Set the value of a var name, if bAddIfNotExists it will be added when it doesn't exist
// oldValue will have the previous value
function bool setValue(string name, string value, bool bAddIfNotExists, optional out string oldValue)
{
  local int i;
  for (i = 0; i<data.length; i++)
  {
    if (data[i].key == name) 
    {
      oldValue = data[i].value;
      data[i].value = value;
      return true;
    }
  }
  if (bAddIfNotExists)
  {
    data.length = data.length+1;
    data[data.length-1].Key = name;
    data[data.length-1].Value = value;
    return true;
  }
  return false;
}
 
// Remove a value from the session
function bool removeValue(string name, optional out string oldValue)
{
  local int i;
  for (i = 0; i<data.length; i++)
  {
    if (data[i].key == name) 
    {
      oldValue = data[i].value;
      data.remove(i, 1);
      return true;
    }
  }
  return false;
}

Example usage

Let's say you have made a custom QueryHandler and you need to keep track of some changes before submitting them.

class MyQueryHandler extends xWebQueryHandler;
 
var Sessions sessions;
 
function bool Init()
{
  Super.Init();
  sessions = new(None) class'Sessions';
  return true;
}
 
function bool Query(WebRequest Request, WebResponse Response)
{
  local session CurSession;
  local int tempvalue;
 
  CurSession = sessions.getSession(Request.GetVariable("sessionID"), true);
  Response.Subst("sessionID", CurSession.hash); // replace %sessionID% with the session hash
 
  tempvalue = int(CurSession.getValue("pageVisits", 0));
  tempValue++;
  tempvalue = CurSession.setValue("pageVisits", string(tempvalue), true);
  Response.Subst("pageVisits", string(tempvalue));
  // process the query
}

Now you will only have to pass the sessionID to remember the variables

Example HTML file:

 <html>
 <body>
 You have visited this page %pageVisits% time(s)
 <p> 
 <a href="?sessionID=%sessionID%">include the session ID in a link like this</a>
 <p>
 or this to include it in a form
 <form>
 <input type="hidden" name="sessionID" value="%sessionID%">
 <input type="submit">
 </form>
 </body>
 </html>

Page Information

2022-11-18T09:46:56.674526Z 2002-12-09T09:41:27Z El Muerte TDS * https://wiki.beyondunreal.com/Legacy:Sessions Attribution-NonCommercial-ShareAlike 3.0