|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
The script below uses Grep to search a flat db file that contains
$username, $company, $redirect fields in the following format, jsmith|Yahoo|yahoo.com jstock|Google|google.com bdoe|Lycos|lycos.com The user enters their "username" into a html form. A "perfect / exact" match will send that person to their specific $redirect variable, otherwise sending them to an error page. The problem that I'm having is if the user doesn't enter their "exact" username, the script is sending the user to the closest match in the string. (If jstock simply entered "js" he might end up at yahoo.com instead of google.com. I hope I've explained myself well and I'm looking forward to your reply(s). Thanks in advance, -= John #!/usr/bin/perl $datafile = "usernames.db"; 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; $value =~ s/~!/ ~!/g; $FORM{$name} = $value; } $searchstr = $FORM{'username'}; open(INF,$datafile); @mydata = <INF>; close(INF); @results = grep(/$searchstr/i,@mydata); if ($#results >= 0) { foreach $i (@results) { chomp($i); ($username,$company,$redirect) = split(/\|/,$i); print "Content-type:text/html\n"; print "Refresh:0; URL=http://www.$redirect/\n\n"; print "</body></html>\n"; } } else { print "No results found.<p>\n"; } print "</body></html>\n"; |
|
#2
|
||||
|
||||
|
If the script must find an EXACT Match in order to redirect them to the correct URL, I would do something like this:
Code:
$searchstr = $FORM{'username'};
$url = undef;
$user = '';
$prefix="http://www.";
open(INF,$datafile) || die $!;
@mydata = <INF>;
close(INF);
CHECK: foreach $user (@mydata) {
chomp $user;
@results = split(/\|/,$user);
if ($searchstr eq $results[0]) {
$url = $results[2];
last CHECK;
}
}
if (defined($url)) {
chomp $url if $url =~ /\n$/;
print ("Location: $prefix$url\n\n");
exit;
} else {
print ("Content-type: text/html\n\n");
print "No results found.<p>\n";
}
Now this will check for an exact username match, generate the redirect url if found. Hope this helps ![]() P.S disregard the .... in the code! Mickalo [Edited by mickalo on 02-01-2001 at 08:28 AM]
__________________
Thunder Rain Internet Publishing Custom Programming & Database development Providing Personal/Business Internet Solutions that work! |
|
#3
|
|||
|
|||
|
Thank You Mickalo! It works perfectly!
|
|
#4
|
||||
|
||||
|
cheers,
Mickalo |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Searching a flat DB file using Grep |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|