通过读取浏览器端的cookie得到sid,Session的保存并不一定是文件(通常是)可以是哈西表,mysql等等,这取决于服务器端的设置。其实你可以直接open(/tmp/xxxx/$sid)在任何时候修改。当然也可以通过,标准的接口修改
代码:
#!/usr/bin/perl use CGI; use Apache::Session::File;
my $query = new CGI; my %session; my $id = undef;
$id = $query->cookie(-name=>"SID01");
tie %session,'Apache::Session::File',$id, { Directory => "/tmp/", LockDirectory => "/tmp/"};
if ($id == undef) { $cookie = $query->cookie( -name=>'SID01', -value=>$session{_session_id}, -expires=>'+1y', -path=>'/session'); print $query->header(-cookie=>$cookie); print "Assigned session ID<br>n"; } else { print $query->header(); print "Not assigned session ID<br>n"; };
if ($query->param()) { $session{"name"} = $query->param("name"); $session{"address"} = $query->param("address"); $session{"birthday"} = $query->param("birthday"); }
print "<html>n"; print " <head><title>Session info</title></head>n"; print " <body bgcolor=#ffffff>n"; print " <form action='/session/userinfo.cgi' method=post>n"; print " <b>Name: </b>"; print " <input type=text size=12 name='name' value='".$session{'name'}."'><br>n"; print " <b>Address: </b>"; print " <input type=text size=12 name='address' value='".$session{'address'}."'><br>n"; print " <b>Birthday: </b>"; print " <input type=text size=12 name='birthday' value='".$session{'birthday'}."'><br>n"; print " <input type=submit></form>n"; print " Your session ID is $idn"; print " </body>n"; print "</html>n";
这里我们先读取用户上次填写的表格内容并显示出来,而当用户按更新时,服务端的资料也就立即被更新
|