#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    New Jersey, USA
    Posts
    27
    Rep Power
    0
    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. #2
  3. Ole` Timer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    N.W. Iowa
    Posts
    472
    Rep Power
    14
    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!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    New Jersey, USA
    Posts
    27
    Rep Power
    0

    Talking


    Thank You Mickalo! It works perfectly!
  6. #4
  7. Ole` Timer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    N.W. Iowa
    Posts
    472
    Rep Power
    14

    Talking No problems mate, glad to help when I can!


    cheers,

    Mickalo

    Thunder Rain Internet Publishing

    Custom Programming & Database development
    Providing Personal/Business
    Internet Solutions that work!

IMN logo majestic logo threadwatch logo seochat tools logo