PDA

View Full Version : How do I get page URL variable into CGI script?


CarolinaWeb
02-14-2005, 10:21 PM
Hello all,
I have a PHP (send this url to a friend) script that I now have set up in a web site that I'm working on that works perfectly. It uses javascript to get the url variable from the web page and posts it into my php form. However, my client has now decided that they want to use a Windows 2000 server rather than Unix, and the Windows server doesn't support PHP. :(
Presently, the javascript is executed when you click on the link "Email this page". Then a form pops up in a seperate window where you can enter your name, email and the recipients email. The url variable from the javascript is passed into this form.
I don't know very much about writing CGI or Perl, so I really need some help! I am hoping to just use a "tell a friend" CGI script that I found online and modify it to get the page url variable. I want to use the same pop up form for about 40 different pages on this web site, hence the need to be able to get the url variable from these different pages into the form somehow.
Can anyone please tell me what I need to have in the html page, cgi script or my form to capture this variable from each page? Your help would be greatly appreciated!
Here is the cgi script that I want to try to modify. As you can see, it only allows for one set url variable to be entered:

#!/usr/bin/perl
# Tell A Friend Script
# (c) Copyright Wayne Pearsall
# webmaster@e-walks.tk
# All rights Reserved
# PS it would be cool if you leave the credit
# for my script on the thanks page :)
############ This is the mainish.. part of the script it is calling
"et_form; # Dont Take out this line else the script will not
############ include the form email address' :)

# Set the location of your servers Sendmail Program
$SEND_MAIL = "/usr/sbin/sendmail";

# Set The URL of Your Website you wish to promote.
$My_Site_Url = "http://www.micradc.com";

# What Email address do you want the Email to come from?
# 1 = your email address 2 = senders email address
$Whos_Email = "2";

# Subject To Use When Telling A Friend $FORM{who_name} is the persons name.
$Tell_Friend_Subject = "$FORM{who_name}, I Just Found An Interesting Site";


# If you Answered 1 to the last variable, Whats your Email?
# Always put a \ before the @ sign.
$My_Email = "my_name\@my_host.com";


# /////////////////////////////////////////////////////
# And Finally Set The HTML code for the thanks Page.
# |||||||||||||||||||||||||||||||||||||||||||||||||||||
# Include $FORM{who_to} to set the email name of the recepiant
# and $FORM{who_from} to set the name of the sender.
# add $FORM{who_name} to add the recievers r/name
# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

$Tell_a_Friend_Text=<<__Stop_Of_Email_Message__; # NOTE! DO NOT EDIT THIS LINE AT ALL!

Hi $FORM{who_name} ,

I have just been to an excellent website,
and thought that you too may enjoy visiting.

The URL to the website is:
$My_Site_Url

Thank You
$FORM{who_from}

__Stop_Of_Email_Message__
# NOTE! DO NOT EDIT ABOVE LINE AT ALL! (as in __Stop_ - above that is fine


# /////////////////////////////////////////////////////
# And Finally Set The HTML code for the thanks Page.
# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


print "Content-type: text/html\n\n";
print <<__End_Of_Thanks_Page__; # NOTE! DO NOT EDIT Above LINE AT ALL!

<HTML>
<HEAD>
<TITLE> Email Sent To Friend </TITLE>
</HEAD>
<BODY>

<H1><CENTER>Thank You!</CENTER></H1>
<H4><CENTER>An Email Has Been Sent To $FORM{who_name} About My Website</CENTER><H4>
<p align="center"><font>Please <A HREF=" $My_Site_Url ">Click Here</A> To Return To My Site</font></p>

<BR>
<BR>
<BR>

<p align="center"><font size="1">Script by <a href="mailto:scripts\@designz.yoll.net?subject=Tell-A-Friend CGI">WPearsall</A>.</font></p>
</BODY>
</HTML>

__End_Of_Thanks_Page__
# NOTE! DO NOT EDIT THIS LINE AT ALL!




###################################################################
# OK - Setup All Done. #
###################################################################


if ($Whos_Email eq "1")
{
$Email_From = $My_Email;
}
if ($Whos_Email eq "2")
{
$Email_From = $FORM{who_from};
}

# End Desision.. Time To Tell The Friend.

open (MAIL, "|$SEND_MAIL -t"); ## Email to the recepiant
print MAIL "To: $FORM{who_to}\n";
print MAIL "From: $Email_From \n";
print MAIL "Subject: $Tell_Friend_Subject \n\n";
print MAIL " $Tell_a_Friend_Text \n";
close(MAIL); ## Finish e-mail to user webmaster



## The Part of the program referred to at the very start
## *remember "et_form; ? Well this reason the email address,
## and recepiants name
sub get_form {

if ($ENV{"REQUEST_METHOD"} eq 'GET') {
$buffer = $ENV{'QUERY_STRING'};
} else {
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;
}
}

Thanks so much for your time and help!

mlseim
02-14-2005, 11:59 PM
On your website, you can keep the button that says "Email this page".
You can keep the Javascript, and the pop up window.

It's in the pop-up window where you need to make a change.
Did you say that the pop-up window is PHP? I didn't understand that part.

If you can get a pop-up window with a form, you can make your Perl
script work.

Maybe you should give us a link to your site so we can see the
Javascript and the pop-up window.

CarolinaWeb
02-15-2005, 03:51 PM
Hi, Thanks so much for responding. Yes, the form is in PHP right now. I'll need to build another one that calls the CGI script that I sent in the previous note. Building the form to call the CGI script isn't a problem. I just need to find out what the code should be to grab the URL variable from the other page. Here is the URL for a page that has the form pop up link.

http://www.rockbridgegroup.com/MiCRA/site/economists/baseman.html

Click on "Email this page" over in the right column. This link has the javascript attached to it, and then the form that pops up grabs the variable from that javascript.

I need to be able to have the same pop up form work for all of the economists bios listed on this page:
http://www.rockbridgegroup.com/MiCRA/site/economists/economists.html

As I said before, presently the form uses PHP, but the client wants to use a Windows 2000 server to host the site that doesn't support PHP.
Any help would be greatly appreciated!

mlseim
02-15-2005, 04:24 PM
OK, you have two php scripts.
The first one is called by the javascript, "friend.php".
That PHP script pops up and has the form on it.
That PHP script then calls another one ... that sends the email.
(I think there's two PHP scripts, but it could be one script ...
we can't see the script "friend.php", so I don't know).

You need to convert both PHP scripts to Perl.

This is how the Javascript calls the PHP script:
document.write("<a href=\"javascript: popUp('../send_fr/friend.php?id=" +document.URL);
document.write("&js=on');\">EMAIL THIS PAGE</a>");

You would create a Perl script called "friend.pl".
and would change the Javascript to:
document.write("<a href=\"javascript: popUp('/cgi-bin/friend.pl?id=" +document.URL);
document.write("&js=on');\">EMAIL THIS PAGE</a>");

The first few lines of your new Perl script would be:
========================================================

#!/usr/bin/perl

use CGI ':standard';

my $url = param('id');

========================================================

That is how Perl takes in the variable from the Javascript.
So, within your script "friend.pl", $url would be the URL that was
grabbed from the Javascript.

If you attached both PHP scripts on this post (as an attachment), we
could see both of them and help you convert them to Perl.

CarolinaWeb
02-15-2005, 09:41 PM
Yeah, I was hoping that I could basically use the same javascript to get the variable into the form and just change out the PHP script for the CGI script. Can I do it this way, or do I have to put the whole form into my CGI script and then call the CGI script from the "Email this page" link? Here are the PHP scripts. I really appreciate the help!

mlseim
02-16-2005, 12:15 AM
... and one other question ...

Do you know how to install a Perl script (CGI script)
into your cgi-bin directory?

You have an FTP program where you can upload
the script in ASCII format and CHMOD the script
permissions? ... and you know what those things
mean.


.... and give me a day to work on this .... thanks

mlseim
02-16-2005, 04:46 AM
I guess the conversion was easier than I thought ... :thumbsup:

You need to change the filename of the attached script to: "friend.pl"

You need to change the javascript lines in your HTML to:
<script language = "JavaScript">
<!--
document.write("<a href=\"javascript: popUp('/cgi-bin/friend.pl?id=" +document.URL);
document.write("&js=on');\">EMAIL THIS PAGE</a>");
//-->
</script><br>

note: I entered the assumed path to your cgi-bin.


There is only one file "friend.pl" ... the configuration is all in that file.
Check the configuration, especially the path to your CSS and Images.
Your Perl script will be in a different directory than your PHP script was ...
the paths need to reflect the new script location. You can try it like
it is (I made assumptions on the location).

Upload the script to your cgi-bin in ASCII mode, CHMOD script to 755.

Alter the email portion ... I wasn't sure if you wanted the part about
"this is an automated message" ... remove it if you want, or re-arrange
the text for your needs.

Here's my working test copy (without your CSS or graphics):
http://www.catpin.com/friend.htm

Give it a try.

And after all that work, I wondered to myself "Are they sure they
wanted the script written in Perl?" Maybe they wanted "ASP" instead. :o
I don't do ASP

Good Luck.

CarolinaWeb
02-16-2005, 03:39 PM
You are the greatest person in the world in my book today! I just uploaded the script and it works just fine...just have to tweak a few things like the styles and images. Thanks sooooooo much!
I'm on a Mac so I use Fetch to FTP. I have installed CGI and Perl scripts that I have gotten from free download sites before, so I know how to CHMOD. I have several books that I bought to learn how to write my own scripts, but haven't gotten this far in the books yet...so much to do, so little time!
Thanks for your time and all of your help! I will definately leave your name and credits in the script on the site! :D

mlseim
02-16-2005, 11:05 PM
One more thing on this ....

If you want to keep a log of "referrals" sent, that would
be easy to do. The script can be modified to append a
line onto a text file each time a link gets sent to someone.

A person could check the log periodically to see how
often it's being used, and which pages are getting
the referrals.

The log could keep track of sender's email and recipient's email.

There's also the possibility of me using your script to send
an anonymous email to someone else using your optional
message box. There probably has been no abuse with that,
but something to think about.

Let me know...

--max--