Solved

Session Information

Posted on 1998-12-17
2
147 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
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 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…

862 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now