CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Finding email address in flat file database...

 



Tim
Deleted

Mar 4, 2000, 2:56 PM

Post #1 of 14 (1980 views)
Finding email address in flat file database... Can't Post

Here is what I have now (I would like a better, more efficient way of doing
this)....

An email address is entered from a form and then their username and password
is emailed back to the email address they entered. It is a flat file
database in this format:

$date;$mememail;$pass;$username

##################################

$lostmail = $FORM{'email'};

open (FILE, "$log")
@users=<FILE>;
close (FILE);

foreach $record (@users) {
chomp $record;
($date,$mememail,$pass,$username)=split(/\;/,$record);
chomp $username;

if ($mememail eq $lostmail) {
&getpassword; #this sub will send out the email
print "Your username and password has been sent to $lostmail";
last;
} else {
print "Your email address was not found in our database.";
exit;
}
}
###################################

Can the above be done with something along these lines?

###################################

open FILE, "+<$log" &#0124; &#0124; &errormessage;
$_ = do { local $/, <FILE> };
$found = s/^\Q$lostmail\E//mg;
close FILE;

if ($found)
{
&getpassword;
print "Information sent to $lostmail"
}
else
{
print "$lostmail not found"
}

#####################################

Any suggestions would be appreciated!!!

Thanks,

Tim


Cure
User

Mar 4, 2000, 5:09 PM

Post #2 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi

There are diferent ways to do it-> heres one way..


$lostmail = $FORM{'email'};
open (DB, "$log") or "couldnt open $!.";
CURE: while(<DB> ){
if (/$lostmail/i)
{
print "Your username and password has been sent to $lostmail";
last CURE;
}
else {
#getpassword();
print "Your email address was not found in our database.";
last CURE;
}
}
close(DB);


Cure

[This message has been edited by Cure (edited 03-04-2000).]


Tim
Deleted

Mar 4, 2000, 5:30 PM

Post #3 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

It also needs to send the user their username and password. Here is a small snippet from my sendmail routine...

print MAIL "Your username is $username\n";
print MAIL "Your password is $pass\n";

So, the email needs to be matched to the appropriate usernamd and password.

Thanks,

Tim


Cure
User

Mar 4, 2000, 5:42 PM

Post #4 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi Tim

$lostmail = $FORM{'email'};
open (DB, ""$log"") or "couldnt open $!.";
CURE: while(<DB> ){
($date,$mememail,$pass,$username)=split(/\;/,$_,4);
if ($mememail =~ /$lostmail/i)
{
print "Your username and password has been sent to $lostmail";
&your_email_subroutine($pass,$username);
last CURE;
}
else {
getpassword();
print "Your email address was not found in our database.";
last CURE;
}
}
close(DB);


Cure


Cure
User

Mar 4, 2000, 10:04 PM

Post #5 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi Tim

This will work:

#!/usr/bin/perl

use CGI;
$q = new CGI;
print "Content-type: text/html\n\n";

#Define Variables
$mail = "/bin/sendmail";
$log = "/home/public_html/script/register.log";
$admin = "mysite\@mysite.com";
$sitename = "My site";
$memberurl = "www.mysite.com/members/";

$lostmail = $q->param('email');

open (DB, "$log") or "couldnt open $!.";
CURE: while(<DB> ){
($date,$mememail,$pass,$username)=split(/\;/,$_,4);
if ($mememail =~ /^$lostmail$/i)
{
print "Your username and password has been sent to $lostmail";
&getpassword($pass,$username);
last CURE;
}
else {
#getpassword();
print "Your email address was not found in our database.";
last CURE;
}
}
close(DB);


###########################################
# MAIL INFO
##########################################
sub getpassword {
open (MAIL, "| $mail $lostmail");
print MAIL "Reply-to: $admin\n";
print MAIL "From: $admin\n";
print MAIL "To: $lostmail\n";
print MAIL "Subject: Lost Password Recovery - $sitename\n\n";

print MAIL "The members url is http://$memberurl\n";
print MAIL "Your username is $username\n";
print MAIL "Your password is $pass\n\n";
print MAIL "Bookmark the following url:\n";
print MAIL "http://$username:$pass\@$memberurl\n\n";
print MAIL "You registered on $date\n";

close MAIL;

}

sub errormessage {
print "The server can't open the logfile. It either doesn't exist or the permissions are wrong. \n";
exit;
}

Cure


Tim
Deleted

Mar 4, 2000, 10:32 PM

Post #6 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi,

That still returned "Your email address was not found in our database."

Here is a sample from my database:

Friday March 3 10:45:16 US/Central 2000;test@defend.net;954693916;tim;

This is really baffling me! Any other ideas?

Thank you much!

Tim


Tim
Deleted

Mar 5, 2000, 8:48 AM

Post #7 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi,

Thanks for your replies.

When I enter an email address, the browser output is always "Your email address was not found in our database." But, if the email address is in the database, I still receive the email. The email contains the information from the first line of the database....even if the email address I entered is on the 10th line.

What changes need to be made? The current code I am using is below:

################################
$lostmail = $FORM{'email'};

open (DB, "$log") or "couldnt open $!.";
CURE: while(<DB> ){
($date,$mememail,$pass,$username)=split(/\;/,$_,4);
if ($mememail =~ /$lostmail/i)
{
print "Your username and password has been sent to $lostmail";
&getpassword($pass,$username);
last CURE;
}
else {
getpassword();
print "Your email address was not found in our database.";
last CURE;
}
}
close(DB);

################################

Thanks,

Tim


Cure
User

Mar 5, 2000, 9:08 AM

Post #8 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi

Im Lost on what your saying-> Are you saying you want this code to see if a user is in the database and if so then send them a email and if there email mail is not in the database then tell them there not in the database.....

Becase the code will work for that..
just comment out the subroutine getpassowrd in the else statment block.


Cure



[This message has been edited by Cure (edited 03-05-2000).]


Tim
Deleted

Mar 5, 2000, 9:23 AM

Post #9 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi,

You're correct in your statement about what I'm trying to do.

As it is now, I continue to get the else statement even though the email is in the database. Here is the script in its entirety. Maybe I have something wrong somewhere else?

##################################
#!/bin/perl

##################################

print "Content-type: text/html\n\n";

#Define Variables
$mail = "/bin/sendmail";
$log = "/home/public_html/script/register.log";
$admin = "mysite\@mysite.com";
$sitename = "My site";
$memberurl = "www.mysite.com/members/";


&form_parse;
$lostmail = $FORM{'email'};

open (DB, "$log") or "couldnt open $!.";
CURE: while(<DB> ){
($date,$mememail,$pass,$username)=split(/\;/,$_,4);
if ($mememail =~ /$lostmail/i)
{
print "Your username and password has been sent to $lostmail";
&getpassword($pass,$username);
last CURE;
}
else {
#getpassword();
print "Your email address was not found in our database.";
last CURE;
}
}
close(DB);


###########################################
# MAIL INFO
##########################################
sub getpassword {
open (MAIL, "| $mail $lostmail");
print MAIL "Reply-to: $admin\n";
print MAIL "From: $admin\n";
print MAIL "To: $lostmail\n";
print MAIL "Subject: Lost Password Recovery - $sitename\n\n";

print MAIL "The members url is http://$memberurl\n";
print MAIL "Your username is $username\n";
print MAIL "Your password is $pass\n\n";
print MAIL "Bookmark the following url:\n";
print MAIL "http://$username:$pass\@$memberurl\n\n";
print MAIL "You registered on $date\n";

close MAIL;

}
################################################################################
# FORM PARSE
################################################################################
sub form_parse {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}}
########################################

sub errormessage {
print "The server can't open the logfile. It either doesn't exist or the permissions are wrong. \n";
exit;
}


Cure
User

Mar 5, 2000, 2:51 PM

Post #10 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi Tim

I tested it 5 times and it worked perfect for me.

Maybe its your html-> did you name your text field email.

Cure


Cure
User

Mar 5, 2000, 3:31 PM

Post #11 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

IM sorry TIM you were right-> i didnt see if it match a email on a line other than the first line.

This will work for sure(sorry)

#!/usr/bin/perl

use CGI;
$q = new CGI;
print "Content-type: text/html\n\n";

#Define Variables
$mail = "/bin/sendmail";
$log = "/home/public_html/script/register.log";
$admin = "mysite\@mysite.com";
$sitename = "My site";
$memberurl = "www.mysite.com/members/";

$lostmail = $q->param('email');

open (DB, "$log") or "couldnt open $!.";
CURE: while(<DB> ){
($date,$mememail,$pass,$username)=split(/\;/,$_,4);
if ($mememail eq $lostmail)
{
print "Your username and password has been sent to $lostmail";
&getpassword($pass,$username);
last CURE;
}
}
close(DB);

print "Your email address was not found in our database." if(!($mememail eq $lostmail));


###########################################
# MAIL INFO
##########################################
sub getpassword {
open (MAIL, "| $mail $lostmail");
print MAIL "Reply-to: $admin\n";
print MAIL "From: $admin\n";
print MAIL "To: $lostmail\n";
print MAIL "Subject: Lost Password Recovery - $sitename\n\n";

print MAIL "The members url is http://$memberurl\n";
print MAIL "Your username is $username\n";
print MAIL "Your password is $pass\n\n";
print MAIL "Bookmark the following url:\n";
print MAIL "http://$username:$pass\@$memberurl\n\n";
print MAIL "You registered on $date\n";

close MAIL;

}

sub errormessage {
print "The server can't open the logfile. It either doesn't exist or the permissions are wrong. \n";
exit;
}

Cure


Tim
Deleted

Mar 5, 2000, 11:25 PM

Post #12 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi,

That worked great! I have a question about one part of it if you don't mind....

=split(/\;/,$_,4);

I don't understand the "$_,4". Could you please tell me a little bit about it?

The other part I don't understand it the "CURE". What is this?

I really appreciate your help!

Tim



Cretep
User

Mar 6, 2000, 1:00 AM

Post #13 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

May I sugest you visit this url to find out all you ever wanted to know about the split function http://www.perl.com/pub/doc/manual/html/pod/perlfunc/split.html

An this url for a full list of perl's functions http://www.perl.com/pub/doc/manual/html/pod/perlfunc.html

Regards

Peter Crouch


Tim
Deleted

Mar 6, 2000, 1:15 AM

Post #14 of 14 (1980 views)
Re: Finding email address in flat file database... [In reply to] Can't Post

Hi,

Thanks for the resources. They'll definitely be useful!!

Tim

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives