Link to home
Start Free TrialLog in
Avatar of wirefram
wirefram

asked on

Check my database? How to?

I need to do this:

I have a database where I enter my data.

Let's say data 1 is ABCD, this code is located on a page (hidden in a button) on another server. When one click on the button that contain the code, I want the user to be sent to a specific page (on another server) ONLY if the code is in my database.

In another words the new page will open ONLY if it can find the code sent from the other page in my database. If not it will open another page.

???
Avatar of flivauda
flivauda

have your scrip that is receiving the submit output different locations if they code is there or not :


#!/usr/bin/perl

&parse_form_data(*FORM);
if ($FORM{'databasecode'} is in my database)
{
  print  "Location:http://www.myhomepage.com/thiswasthere.html\n\n";
}
else
{
  print  "Location:http://www.myhomepage.com/thiswasnotthere.html\n\n";
}


sub parse_form_data
{
    local (*FORM_DATA) = @_;
    local ( $query_string, @key_value_pairs,
                          $key_value, $key, $value);

    read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'});

    @key_value_pairs = split (/&/, $query_string);
    foreach $key_value (@key_value_pairs) {
        ($key, $value) = split (/=/, $key_value);
        $value =~ tr/+/ /;
        $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;

        if (defined($FORM_DATA{$key})) {
            $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value);
        } else { $FORM_DATA{$key} = $value;
        }
    }
}

Avatar of wirefram

ASKER

I am totally new with Perl.
Your form looks good but where do I write the name of my database for the form to check?

My database is data.txt


This is the beginning of my script, at some point I should be able to insert the function you provided but could you explain in details what I have to typed exactly?
++++++++++++++++++++++++++++++++++
$current_time = time();

$temp=$ENV{'QUERY_STRING'};
@pairs=split(/&/,$temp);
foreach $item(@pairs) {
      ($key,$content)=split (/=/,$item,2);
      $content=~tr/+/ /;
      $content=~ s/%(..)/pack("c",hex($1))/ge;
      $fields{$key}=$content;
}
$r=0;
if ($fields{'ID'} == 0) {
open (DAT,"<members.db");
if ($use_flock) {
flock DAT, 2;
}
@database_array = <DAT>;
close (DAT);
&thehtml;
}
unless ($fields{'ID'}) {

print "Location: $addsite_location/welcome.html\n\n";
exit;
}

open (DAT,"<members.db");
if ($use_flock) {
flock DAT, 2;
}
@database_array = <DAT>;
close (DAT);


&member_in;
exit;
+++++++++++++++++++++++++++

If the ID is found I want to open the "welcome.html" if the ID is not found I want to open "wrongpage.html".

members.db is the database


if ($FORM{'databasecode'} is in my database)

this line is what you need to use your code for. the $FORM{'databasecode'} is just what i was reading out of the html form element databasecode.

so run your code to decide if it is in your database and set a flag when you find it:

$found = 0;
.
code checking if it is in the database
if the item is in the database { $found = 1; }

then you would have

     if ($found == 1)
     {
       print  "Location:http://www.myhomepage.com/welcome.html\n\n";
     }
     else
     {
       print  "Location:http://www.myhomepage.com/wrongpage.html\n\n";
     }
Well, can't say I feel better, if you look at the beginning of my script, where do I type your stuff?

Do you have a real example using the same names than in my script?  
if ($FORM{'databasecode'} is in my database) //is it what I have to replace with ($fields{'ID'})  in my script? Do I really write "is in my database" or is it something like "=1"

???I told you I never did a cgi before and i am using a free script that I try to adjust to my needs!
do whatever code you need to, to determine if the item is in your database if you find it set a flag.  then change the if .. is in my datase to
if ($flag == 1)
Can you look at the beginning of my script (posted on my 3rd message) and tell me exactly where I write the flag stuff.

I assume it's in this part of the script because it contain

print "Location: $addsite_location/welcome.html\n\n";

Somewhere in this area I should have something that explain to open another page if not found.

Honestly set a  flag is above my actual understanding of perl!

Sorry, I look stupid! (and feel the same)

Can someone look at my script (posted previously) and tell me exactly where to insert the code that check the ID?

Thanks

ASKER CERTIFIED SOLUTION
Avatar of inkumc
inkumc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I haven't try yet but what you say is totally logical, in the htaccess I can redirect to the page I want (I think?)

I'll try that.

Thanks
No problem...