Solved

ozo, pcuser 321.. HELP!?

Posted on 2000-02-15
8
173 Views
Last Modified: 2010-03-05
Hi,
Here is a script that is supposed to take your email,password and score and then write that your line of a txt file. Then it is supposed to check the high scores file and if your score is higher than any of them, put yours in and bump the rest down by one. Then it it supposed to write your alias and score to a lastpost file so i know who the last person that posted was. Note: on the lastpost file, i dont want to append, just overwrite the existing.

After all that is done, i just want the user to be redirected to my main page.

It seemed to be going ok but i am having A LOT of trouble trying to get the bugs out. Yes i know the script is extremely messy but im just starting. (if thats an excuse). Would you please look at the script and tell me why it wont execute?

#!/usr/bin/perl
use CGI;

$query= CGI->new();
$email = $query->param('email');
$passwd = $query->param('password');
$score = $query->param('score');
$email=lc($email);
$passwd=lc($passwd);

if ($email eq ""){&leave;}
if ($passwd eq ""){&leave;}

sub leave{
print "Content-type: text/html\n\n";
print "Something is wrong. Please hit your back button and try again.\n";
exit;
}

&checkpass;

sub checkpass{
$file = "reg_".substr($email,0,1).".txt";
open (FILE,"$file");
@people = <FILE>;

foreach $line (@people){
      chomp($line);
      ($mail,$pass,$score,$alias) = split /\|/, $line;
      if ($email eq $mail) {
         if ($passwd eq $pass) {&decryptscore;} else {&nomail;}
      }
      else {&nomail;}
}

close(FILE);
}

sub nomail{
print "Content-type: text/html\n\n";
print "Invalid login. Are you sure you are registered? Click <a href=\/directory/register\.htm>HERE<\/a> to register or your back button to try again.\n";
}

sub decryptscore{
$pattern="(2x)(9-E)(30X)(9-A)(10X)(9-D)(20X)(9-H)(9X)(9-J)(16X)(9-B)(6X)(9-G)(9-F)(36X)(9-C)(84X*)(9-I)(X)";

$pattern=~s/\((\d*)X\*\)/z$1/gi;
$pattern=~s/\((\d*)X\)/x$1/gi;
$pattern=~s/\(9-(\w)\)/$1/g;

@N = $pattern=~/([A-Z])/g;
($p84=$pattern) =~ tr/zxA-Z/axx/;
($px=$pattern) =~ tr/zxA-Z/xax/;
($pn=$pattern) =~ tr/zxA-Z/xxa/;

$px =~ s/a9/x9/;


$x84=unpack $p84,$score;

$x = substr((join'',unpack $px,$score),0,length $x84);
$x=~s/(\d)/9-$1/eg;

if ($x eq $x84){
@n{@N} = unpack $pn,$score;
$dscore=(9x@N)-join'',@n{'A'..'J'};
&writeit;
&checkhigh;
&writelast;
} else {&cheater;}
}

sub cheater{
print "Content-type: text/html\n\n";
print "The encrypted score is invalid.\n";
exit;
}

sub writeit{

$file = "reg_".substr($email,0,1).".txt";
open (FILE,"$file");
open (TMPFL,">>temp.tmp");
@ids = <FILE>;
foreach $line (@ids){
chomp($line);
($mail,$pass,$score,$alias) = split /\|/, $line;
  if ($mail ne $email) {print TMPFL "$line\n";} else {
$theline = $email."|".$passwd."|".$dscore."|".$alias;
print TMPFL "$theline\n";}
}
close(TMPFL);
close(FILE);
unlink($file);
rename "temp.tmp",$file || die("Unable to rename.");
}

sub checkhigh{
$file = "high.txt";
open (FILE,"$file");
@people = <FILE>;

foreach $line (@people){
      chomp($line);
      ($no,$alia,$scor) = split /\|/, $line;
      if ($score > $scor) {$l = 1}}
close(FILE);
if ($l == 1){&writehigh;}
}

sub writehigh{
$nom = 0;
$file = "high.txt";
open (FILE,"$file");
open (TMPFL,">>high.tmp");
@ids = <FILE>;
  foreach $line (@ids){
  chomp($line);
  $nom++;
  ($no,$ali,$sco) = split /\|/, $line;
  $line = $nom."|".$ali."|".$sco;
    if ($sco > $scor) {print TMPFL "$line\n";} else
        {
        $theline = $nom."|".$alias."|".$dscore;
        print TMPFL "$theline\n";
        }
 }
close(TMPFL);
close(FILE);
unlink($file);
rename "high.tmp",$file || die("Unable to rename.");
}

sub writelast{
$file = "lastpost.txt";
open (FILE,">$file");
$theline = $alias."|".$dscore;
print FILE "$theline\n";
close(FILE);
print "Location: http:\/\/www\.mydomain\.com\/directory\/index1\.shtml\n\n";
exit;
}
0
Comment
Question by:MAXcom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:MAXcom
ID: 2525178
sorry, forgot to include an encrypted scring for testing:
32905634009932754887810741732407198258806730962982965574976507206624257560046060088651698145973700578074755623815890827210070269201614160967943659900672451121892582675928174119326937017034425023492793393991134830185426299451
0
 
LVL 5

Accepted Solution

by:
PC_User321 earned 0 total points
ID: 2526096
Your problem is that in this line
   ($mail,$pass,$score,$alias) = split /\|/, $line;
you destroy the value of $score that was set earlier.
Change that line to
   ($mail,$pass,$notUsed,$notUsed) = split /\|/, $line;

I did not try to debug your 'high score' logic because your code was rather complex.  Does it work?  If not then rewrite it as follows:-
Entries in high.txt should be in the form
   score|alias
where score HAS ALL LEADING ZEROS SHOWN.
Then do this:-
   Read file into array.
   Sort array
   If new score is higher than the first (=lowest) score in the array then 'shift' that low score out of the array and 'push' the new value in. Then write the array into the file, replacing the old contents.
   

0
 

Author Comment

by:MAXcom
ID: 2527974
Adjusted points to 9
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:MAXcom
ID: 2527975
Hi,
Thanks a lot. That worked. I didnt realise that overwriting it would cause a problem.
The High Scores are kept as:

rank(1-10)|Alias|Score

this is the only part that apparently didnt work. How do you sort an array?
0
 

Author Comment

by:MAXcom
ID: 2528120
nevermind, i got it working the way i had it.
thanks
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2529343
Thanks for the points.

This is what I was talking about
(lots simpler than yours):-


# All scores in scores high.txt are stored like the quoted text below.
# Here is the new score that we want to test
#
$NewScoreAndAlias = "0000123333|PC_User321";
($NewScore, $NotUsed) = split (/|/, $NewScoreAndAlias);

# Read the high scores and add the new one to the list
open (SCORES, "+< high.txt");
@Scores = <SCORES>;
push @Scores, "$NewScoreAndAlias\n";

# Sort the scores and dump the worst one
@Scores = sort @Scores;
shift @Scores;                  # Push out the lowest score

# Don't know why the program wants these
close (SCORES);
open (SCORES, "> high.txt");

# Write back to file
print (SCORES @Scores);
close (SCORES);


0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2529415
The ranking (1st, 2nd,..) need not be in the file because the line number in the file gives that info.
0
 

Author Comment

by:MAXcom
ID: 2529427
amazing.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

624 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