Solved

Session Information

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
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…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

14 Experts available now in Live!

Get 1:1 Help Now