Welcome to my little page provided to VNWR by Mr Boo!
This page contains all the information you need to setup a cgi file to collect email addresses into a txt file. CGI is no easy thing to understand and I myself am still struggling to learn it. This page will hopfully give you an insite into how CGI can be used and how poerful it is!! CGI can do many things on the web. You can use it to store information, send information, retrieve information. On this page we will be looking at retrieving information from a surfer. (Your horny surfers with their pants half down). A good way to help keep traffic is to setup a mailing list. There can be implementations doing this but if you do it legally then there will be no problems :o) OK onto the code :
#!/usr/bin/perl
print "Content-type:text/html\n\n";
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/\n/ /g; # replace newlines with spaces
$value =~ s/\r//g; # remove hard returns
$value =~ s/\cM//g; # delete ^M's
$FORM{$name} = $value;
}
$datafile
= "data.txt";
$email = $FORM{'email'};
if(not($email
=~ /^\@|\@/)) {
&dienice("Please Provide A Correct Email Address $!");
}
open(OUTF,">>$datafile")
or dienice("Couldn't open $datafile for writing: $!");
flock(OUTF,2);
seek(OUTF,0,2);
print OUTF "$email\n";
close(OUTF);
print
<<EndHTML;
<html><head><title>Thank You</title></head>
<body>
<h2>Thank You!</h2>
Thank you for your feedback.<p>
<a href="index.html">Return to our home page</a><p>
</body></html>
EndHTML
sub
dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
I made the above code to do a number of things. Let me run through the script
with you. At the top of any perl CGI script you must put the location of perl
on your server. This can be tricky to find out but the most common location
used is /usr/bin/perl. If you do not know this you can ask your server admin
folk to give you this info. It will take them noless then 10 secs to dish out
the inf so dont worry about wasting their time :o) We place this information
as follows:-
#!/usr/bin/perl
We are going to want to use some sort of html in our script to allow us to display messages in the surfers browser. We tell the script this by typing the following :
print "Content-type:text/html\n\n";
Next is a very tricky bit of code to explain. Lets take a little look at it:
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/\n/ /g; # replace newlines with spaces
$value =~ s/\r//g; # remove hard returns
$value =~ s/\cM//g; # delete ^M's
$FORM{$name} = $value;
}
Most CGI files have this code. Without going into much detail all it does is collect information from forms and stores them as temporary variables. Lets say you had a form where you have an input text box with the value email. <INPUT TYPE="TEXT" NAME="EMAIL"> The CGI would take the data from the form and store it in memory as $FORM{'email'}
$datafile
= "data.txt";
$email = $FORM{'email'};
The above 2 lines of code set the name of the file you would like to store the emails in and also stores the variable $FORM{'email'} as $email for easier reference.
if(not($email
=~ /^\@|\@/)) {
&dienice("Please Provide A Correct Email Address $!");
}
We want to keep our datafile as clean as possible without having to check it every 5 mins to see if everything is looking ok. the above code simply checks the syntax of teh submitted email address. It makes sure that the email address is set out as something@something.com. This is helpful cause you dont want people to be able stupid things like iamadickatprickdotcom. Im sure it has happened LOL!! Anyways if the email address submitted was not valid it would return the error "Please Provide A Correct Email Address". It does this by useing a sub routine called dienice which is coded later on in the script. We will come back to this further down the page....if you ever make it down there cause this is probably getting a little boring LOL.
open(OUTF,">>$datafile")
or dienice("Couldn't open $datafile for writing: $!");
flock(OUTF,2);
seek(OUTF,0,2);
print OUTF "$email\n";
close(OUTF);
OK this is the section of teh script which opens the datafile specified in the variable $datafile. In the first line you can see that the command dienice comes into play again. The eroor messgae displayed will be displayed if the datafile is currently in use or is having problems opening. The next 2 lines lock the file so that once it has been opened nothing else can be added. then it prints the variable $email and closes the database.
print
<<EndHTML;
<html><head><title>Thank You</title></head>
<body>
<h2>Thank You!</h2>
Thank you for your feedback.<p>
<a href="index.html">Return to our home page</a><p>
</body></html>
EndHTML
After the surfer has submitted their email address you might want to thank them or at least acknowledge that you have recieved it. The code above simply displays a message saying thankyou and displays a path to another page of your site. This code can be changed as much as you like using HTML. If you only change the info from <<EndHTML; to EndHTML you will not have any probs :o)
sub
dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
The last section of code in the script is the dienice routine mentioned earlier. It has been setup to diplay a certain message (associated with the problem).
Very nice Boo but how the fuck do I get it to work?We are going to need a form to get information from. The form is very simple to setup. We place the following code on our HTML pages :
<FORM
METHOD="post" ACTION="cgifile.cgi">
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
</FORM>
Of course this can be edited. Make sure that the ACTION tag is set to the location of the CGI file we are creating :o)
Setting up the CGI file!I think that the best program to make CGI scripts in is notepad. That way the code will not get messed up. CGI is a lot more sensitive than HTML. If you add 1 character out of place it will not work :o(
Once you have the
code in notepad you can save the file as anythingyouwant.cgi. When uploading
to your server you have to make sure to set the permissions to 755 which allow
the surfers to run the script. you will also need to make a data file. Usually
the best thing to do is make this in notepad the same way as you did the CGI
file but call it something like *.txt. The permissions of this file should be
set to 644 which will allow only the server to write and access the file :0)
Thats it!!!
Thats it!! Well I dont know how much help that way exactly but maybe someone out there learnt something LOL. I think that the best thing to do would be experiment with the code. Add variables and stuff. Also try to setup a cgi file to take the email addresses and send out emails to all those on the list. This unfortunataly you will have to do by yourself as Boo has gotta run and make some more porn pages........................feel free to contact me if you have any commemts/suggestions or questions :o)
EMAIL : webmaster@fat-tgp.com
ICQ = 98656097