By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

Website Session Tracking - No Builtin Modules

P: 2
Hi all

My problem is that I am creating a session file on the server when the user first visits my website, but when the user logs in i want to write to the file again with the timestamp and their userID.

Code snipet

Expand|Select|Wrap|Line Numbers
  1. if ($core->cookie(-name=>'ChasSession') eq "") {
  2.     if ($sID eq "") {
  3.         $sID = int(rand 1e9);
  4.     }
  5.     else{
  6.         $sID = $core->param('sID');
  7.     }
  8.     $cookie = $core->cookie(
  9.         -name        => 'ChasSession',
  10.         -value        => $sID,
  11.         -expires    => '+7d',
  12.     );
  13.     $file = "session.$sID";
  14.     printHeader("true");
  15. }
  16. else{
  17.     printHeader();
  18. }
  19.  
  20. open (SESSION, ">$file") ;
  21. flock SESSION, "2";
  22. print SESSION "lastActive:". localtime()."\n";
  23. print SESSION "userID: $userID";
  24. flock SESSION, "8";
  25. close (SESSION);
  26.  

this is the first run through it does this
and this works as I want it to.

But when i call a sub (when the user submits their username and password) and try to re-write to the file it doesn't work.

Expand|Select|Wrap|Line Numbers
  1. sub CorrectLogin {
  2.     open(LOGIN, "<.htpasswd");
  3.     flock LOGIN, "2";
  4.     $user = shift;
  5.     $pwd = shift;
  6.     $page = shift;
  7.  
  8.     if (($user eq "") || ($pwd eq "")) {
  9.         printDefaultBody("1");
  10.     }
  11.     else{
  12.         while (<LOGIN>){
  13.             @userpwd = split(":", $_);
  14.             chomp;
  15.             $tmpUser = @userpwd[0];
  16.             $tmpPwd = @userpwd[1];
  17.             $tmpPwd =~s/\s*$//g;
  18.             $salt = substr(@userpwd[1], 0,2);
  19.             if (($user eq $tmpUser) && (crypt($pwd, $salt) eq $tmpPwd)) {
  20.                 open (SESSION, "<$file");
  21.                 rename ("$file", "$file.old");
  22.                 open (NEWSESSION, ">$file");
  23.                 while (<SESSION>) {
  24.                     print NEWSESSION "lastActive:". localtime()."\n";
  25.                     print NEWSESSION "userID: $user";
  26.                 }
  27.                 login($user);
  28.                 printPageX($page, $file);
  29.                 return;
  30.             }
  31.         }
  32.  
  33.         if (($user ne $tmpUser) || (crypt($pwd, $salt) ne $tmpPwd)) {
  34.             print "<div class = \"error\">Invalid username and password </div>";
  35.             printDefaultBody("1");
  36.         }
  37.     }
  38.  
  39.     flock LOGIN, "8"; #unlock
  40.     close(LOGIN); #close file
  41. }
  42.  
Hope this is enough information for somebody to help me.

Charles


EDIT: It is for an assignment for University and I am not allowed to use any of the inbuilt modules
May 21 '07 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
try chomping before splitting:

while(<LOGIN>){
chomp;
@userpwd = split(":", $_);
###chomp;
May 21 '07 #2

P: 2
The login part works fine too, what my problem is second time i use SESSION file handle, it doesn't write to the file that is created previously.
May 22 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
add some debugging and see if it helps:

Expand|Select|Wrap|Line Numbers
  1. open (NEWSESSION, ">$file") or die "can't open $file: $!";
nearly all system commands, such as open, should be checked for success or failure as a habit.
May 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.