Solved

Session Information

Posted on 1998-12-17
2
149 Views
Last Modified: 2013-12-25
How do i send some information across pages in a session in CGI? i want the information to be persistent in a user session.
0
Comment
Question by:britto_nl
2 Comments
 
LVL 1

Accepted Solution

by:
flivauda earned 70 total points
ID: 1830175
You have two options you can set a cookie or you can pass it around through the query string. I would set a  cookie.
==================
# Here is how you would set the cookies
&setMemberCookie($username);    # expires in one day
&setMemberCookie($username, 1); # expires in one year

# Here is how you would read the cookies
&getMemberCookie(*COOKIE);


sub setMemberCookie
{ local ( $username, $permanentcookieflag) = @_;

  $cookie = uname=$username&otherinfo=$otherinfo;
  $expiration = &getCookieTimestamp($permanentcookieflag);
  print "Set-Cookie: login=$cookie; expires=$expiration; path=/; domain=.yourdomain.com;\n";
  $ENV{'HTTP_COOKIE'} = "login=$cookie; $ENV{'HTTP_COOKIE'}";
}


# getCookieTimestamp
#
# returns a timestring in the format:
# Weekday, DD Month YEAR HH:MM:SS GMT
#
# if the call to setMemberCookie contains a permanentcookie
# flag that is true, then the cookie will expire in one year.
# otherwise the cookie will expire in one day.
#
#############################################################

sub getCookieTimestamp{
    local ( $permanentcookieflag) = @_;
    local ($months, $weekdays, $ampm, $time_string, $cookietimeout);

    if ( $permanentcookieflag )
      { $cookietimeout = 60 * 60 * 24 * 365;
      }
    else
      { $cookietimeout = 60 * 60 * 24;
      }

    local ($sec, $min, $hour, $day, $nmonth, $year, $wday, $yday, $isdst) = gmtime(time+$cookietimeout);

    $months = "Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec";
    $weekdays = "Sun/Mon/Tue/Wed/Thu/Fri/Sat";

    if ($hour > 12) { $hour -= 12; $ampm = "pm"; }
    else { $ampm = "am"; }

    if ($hour == 0) { $hour = 12; }
    $year += 1900;
    $week  = (split("/", $weekdays))[$wday];
    $month = (split("/", $months))[$nmonth];

    $time_string = sprintf("%s, %s %s %s %02d:%02d:%02d GMT", $week, $day, $month, $year, $hour, $min, $sec);
    return $time_string;
}



# getMemberCookie
#
# if the login=blablabla cookie is set, decode it and return
# the pointer to the hash.  otherwise return false.
#
#############################################################

sub getMemberCookie
{ local (*COOKIE_DATA) = @_;
  local ( $cookie_string, @key_value_pairs, $key_value, $key, $value);

  if ( $ENV{'HTTP_COOKIE'} =~ /login=([^;]+)/ )
  {
  }
  else
  {
    return("false");
  }

  @key_value_pairs = split (/&/, $cookie_string);
  foreach $key_value (@key_value_pairs)
    { ($key, $value) = split (/=/, $key_value);
      $value =~ tr/+/ /;
      $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;

      if (defined($COOKIE_DATA{$key}))
        { $COOKIE_DATA{$key} = join ("\0", $COOKIE_DATA{$key}, $value);
        }
      else
        { $COOKIE_DATA{$key} = $value;
        }
    }

   return %COOKIE_DATA;
}


0
 

Author Comment

by:britto_nl
ID: 1830176
Adjusted points to 75
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question