Solved

Session Information

Posted on 1998-12-17
2
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

749 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