Solved

ozo, pcuser 321.. HELP!?

Posted on 2000-02-15
8
165 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
  • 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

20 Experts available now in Live!

Get 1:1 Help Now