473,378 Members | 1,066 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Website Session Tracking - No Builtin Modules

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
3 1557
KevinADC
4,059 Expert 2GB
try chomping before splitting:

while(<LOGIN>){
chomp;
@userpwd = split(":", $_);
###chomp;
May 21 '07 #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
4,059 Expert 2GB
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

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

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys, Im about to build a script which will log visitor time spent on my website. I got a few ideas about this, maybe checking visitors ip and storing that info in db with time in and then...
1
by: marslee | last post by:
I am a php newbie. I would like to count how many times a user visit a webpage. I know session tracking should be used, but where i put the code? Is it inside the html that the user visit? ...
2
by: David Frauzel | last post by:
I'm writing a rough draft of a suite of small Perl apps that will, combined, serve as a web site's user sign-up, login, management, and customized content engine. I'm having trouble with the...
10
by: Harry Slaughter | last post by:
I've got a client who wants to see some immediate results on a brand new website. within a week, they'd like to see the following: 1) basic user authentication (using php sessions/cookies to...
2
by: Casey Hawthorne | last post by:
Is there a way to determine -- when parsing -- if a word contains a builtin name or other imported system module name? Like "iskeyword" determines if a word is a keyword! -- Regards, Casey
3
by: C G | last post by:
Dear All, I wonder if anyone can advise me with this problem. 1. A user logs into the database (through web, webservice, some other piece of software) - connect(user="joe",passwd="blogs") 2....
1
by: DDK | last post by:
Does anyone know if there are any books that talk about building a website tracking program for asp.net and C#? Or even any good resources on the web that talk about this. Thanks for any info,...
43
by: davidkoree | last post by:
I mean not about cookie. Does it have something to do with operating system or browser plugin? I appreciate any help.
0
by: =?Utf-8?B?YWRhdmlzLmRldg==?= | last post by:
Currently, we have an ASP.NET 1.1 application where there exists several "sections". We are upgrading the application to .NET 2.0 and want to seperate these sections into individual VS projects...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.