Solved

Returning to previous page

Posted on 2000-03-28
12
441 Views
Last Modified: 2013-12-25
This question is really for Lambda, and is a refinement of an earler CGI question which he helped solve.

If you will go to this page (a website I'm developing) you will see an email newsletter button on the bottom right.

http://www.mdsutton.com/auto-1.html

After the person fills out their name and email address they are sent to a "thank you" page -- with a button that they click that will send them back to whatever page they were previously on.  This email newsletter subscription is on almost every other page of this webiste, in different directories, etc.

But the "Thank You" page script sends them back to the right page.

Here is the script (I slightly modified from the BigNoseBIrd website) that I used to make this happen.  I would like this "return to previous page" feature added to the "Email Article" script that you helped modify and get working.

(FOllowing that is the final verison of the "email article" script from the
previous CGI Experts exchange question)
#! /usr/local/bin/perl
# Be sure that the line above points to where perl 5 is
# on your system.  Modified by rowby

##################################################################
# subscribe.cgi: subscription e-mail collector with subscribe
# and unsubscribe features.
# Release 1.0 on 04/17/99
# (C) 1999 BigNoseBird.Com, Inc. This program is freeware and may
# be used at no cost to you (just leave this notice intact).
# Feel free to modify, hack, and play  with this script.
# It is provided AS-IS with no warranty of any kind.                
# We also cannot assume responsibility for either any programs      
# provided here, or for any advice that is given since we have no  
# control over what happens after our code or words leave this site.
# Always use prudent judgment in implementing any program- and      
# always make a backup first!                                      
#
##################################################################

# When calling the script, you must provide the following INPUTs
#
#   datafile  (name of file that will contain the addresses.)
#   email  (the e-mail address of the person subscribing/unsubscribing
#   action (subscribe or unsubscribe)

#### USER CONFIGURATION SECTION ##################################

# set BASEDIR to the directory that will hold your letter and
# mailling list files. Be certain that the script has permission
# to write to this directory. This must be set to the same value
# you declare in nmmdadmin.cgi

   $BASEDIR="/web/guide/mdsutton/cgibin/";

# remove the # mark before okaydomains to restrict subscription
# requests to your web site. This prevents others from calling
# your script from elsewhere. If you encourage others to offer
# your newsletter from their sites, do NOT remove the # mark.

#   @okaydomains=("http://mydomain.com", "http://www.mydomain.com");    

# $delimiter is the special character that is used to separate the
# items of information about each e-mail address. To use the TAB
# character, uncomment (remove the # mark) the line that says TAB
# and place a # mark at the start of the line that says PIPE.

   $delimiter="\\|"; #PIPE
#   $delimiter="\\t"; #TAB

##################################################################

   $lockfile="/tmp/subscribe.lck";

   &valid_page;
   &decode_vars;
   $return_to=$ENV{'HTTP_REFERER'};
   $the_date=localtime();
   $ip_addr=$ENV{'REMOTE_ADDR'};
   $datafile="$fields{'datafile'}\.mbz";
   $email=$fields{'email'};
   $action=$fields{'action'};

   if ($datafile eq "")
     { print "Content-type: text/html\n\n";
       print "Configuration Error: No datafile specified\n";
       exit;
     }
   if ($action eq "")
     { print "Content-type: text/html\n\n";
       print "Configuration Error: No action specified\n";
       exit;
     }

   if (&valid_address($email) == 0)  
     {
      &bad_email;
      exit;
     }

   &write_data;
   &thank_you;

sub thank_you
{
  if ($action eq "unsubscribe")
    { $whichaction = "removed from";}
    else { $whichaction = "added to";}
print "Content-type: text/html\n\n";
print <<__END_THANKS__;
<body background="http://www.mdsutton.com/images/line-bg1.gif">

<CENTER>
<TABLE WIDTH="510" BORDER="0" CELLPADDING="3" BGCOLOR="#FFFFFF">
</table>
</center>
<div align="center">
  <center>
  <table>

 <TR>
 <TD>
  <TABLE WIDTH="637" BORDER="0" CELLPADDING="5" BGCOLOR="#FFFFFF">
   <TR>
    <TD width="520">
      <CENTER>
      <FONT FACE="ARIAL">
      <font color="#800000" size="4" face="ARIAL"><b>
      &nbsp; Thank You!</b></font>
      <p>
      <B><font color="#000080">You have been $whichaction the M.D. Sutton
      Newsletter e-mail list.&nbsp; <br>
      <i>You will receive your first issue soon.</i></font><font color="#800000"><br>
      <BR>
      </font>
      <A HREF="$return_to"><img border="0" src="http://www.mdsutton.com/images/click-prev-page.gif" width="192" height="86">
    </TD>
    <TD width="117" valign="middle">
      <FONT FACE="ARIAL">
     
<img border="0" src="http://www.mdsutton.com/images/mike-with-name.gif" align="left" width="106" height="140">
    </TD>
   </TR>
  </TABLE>
  </TD>
 </TR>
    </table>
   </center>
  </div>

__END_THANKS__
}

##################################################################
sub write_data
{
   &get_datetime;
   $delim=$delimiter;
   $delim=~s/\\//g;
   &get_the_lock;
   open(IDBFILE,"<$BASEDIR$datafile");
   @mailing=<IDBFILE>;            
   close(IDBFILE);            

   open(ODBFILE,">$BASEDIR$datafile");
   foreach $line (@mailing)    
    {
      chop $line;
      ($thismail,$thisip,$thisdate)=split(/$delimiter/,$line);
      if ($email ne $thismail)
       {
        print ODBFILE "$line\n";
       }
    }
   if ($action eq "subscribe")
    {
     print ODBFILE "$email$delim$ip_addr$delim$rd$delim\n";
    }
   close (ODBFILE);
# This may seem a silly place to check to see if we were able to
# create the file, but a lot of people don't have access to their
# error logs to find message written by "die"
   &drop_the_lock;
   if (!-w "$BASEDIR$datafile")
     { print "Content-type: text/html\n\n";
       print "Configuration Error: could not create datafile
              please check path and permissions!\n";
       exit;
     }

}

##################################################################
sub decode_vars
 {
  $i=0;
  read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
  @pairs=split(/&/,$temp);
  foreach $item(@pairs)
   {
    ($key,$content)=split(/=/,$item,2);
    $content=~tr/+/ /;
    $content=~s/%(..)/pack("c",hex($1))/ge;
    $content=~s/\t/ /g;
    $content=~tr/A-Z/a-z/;
    $fields{$key}=$content;
   }
}

##################################################################
sub valid_address
 {
  local($testmail) = @_;
  if ($testmail eq "")
   {return 0;}
  if ($testmail =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
  $testmail !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/)
   { return 0;}
   else
    { return 1;}
}

##################################################################
sub bad_email
{
print <<__STOP_OF_BADMAIL__;
Content-type: text/html

<body background="http://www.mdsutton.com/images/line-bg1.gif">

<CENTER>
<TABLE WIDTH="510" BORDER="0" CELLPADDING="3" BGCOLOR="#FFFFFF">
</table>
</center>
<div align="center">
  <center>
  <table>

 <TR>
 <TD>
  <TABLE WIDTH="637" BORDER="0" CELLPADDING="5" BGCOLOR="#FFFFFF">
   <TR>
    <TD width="520">
      <CENTER>
      <FONT FACE="ARIAL">
      <font color="#800000" size="4" face="ARIAL"><b>
      &nbsp; Sorry!&nbsp;</b></font>
      <B><font color="#000080"><br>
      Your request to join the M.D. Sutton Newsletter could not be processed because your email address was not properly entered. Please enter it this way:
      &quot;yourname\@somewhere.com&quot;. <br>
      </font><font color="#800000">
      <BR>
      </font>
      <A HREF="$return_to"><img border="0" src="http://www.mdsutton.com/images/click-prev-page.gif" width="192" height="86">
    </TD>
    <TD width="117" valign="middle">
      <FONT FACE="ARIAL">
     
<img border="0" src="http://www.mdsutton.com/images/mike-with-name.gif" align="left" width="106" height="140">
    </TD>
   </TR>
  </TABLE>
  </TD>
 </TR>
    </table>
   </center>
  </div>
__STOP_OF_BADMAIL__
}

##################################################################
sub get_the_lock
{
  local ($endtime);                                  
  $endtime = 60;                                      
  $endtime = time + $endtime;                        
  while (-e $lockfile && time < $endtime)
   {
    # Do Nothing                                    
   }                                                  
   open(LOCK_FILE, ">$lockfile");                    
}

##################################################################
sub drop_the_lock
{
  close($lockfile);
  unlink($lockfile);
}

##################################################################
sub get_datetime
{
   %mos = ( "jan","01", "feb","02", "mar","03", "apr","04",            
            "may","05", "jun","06", "jul","07", "aug","08",
            "sep","09", "oct","10", "nov","11", "dec","12");
   $date=localtime(time);                                              
   ($day, $month, $num, $time, $year) = split(/\s+/,$date);            
   @time_temp=split(/\:/,$time);                                        
   $month=~tr/A-Z/a-z/;                                                
   $rd="$mos{$month}\/$num\/$year $time_temp[0]\:$time_temp[1]\:$time_temp[2]";
}


##################################################################
sub valid_page
 {
  if (@okaydomains == 0)
     {return;}
  $DOMAIN_OK=0;                                        
  $RF=$ENV{'HTTP_REFERER'};                            
  $RF=~tr/A-Z/a-z/;                                    
  foreach $ts (@okaydomains)                            
   {                                                    
     if ($RF =~ /$ts/)                                  
      {                                                
        $DOMAIN_OK=1;                                  
      }                                                
   }                                                    
   if ( $DOMAIN_OK == 0)                                
     {                                                  
      print "Content-type: text/html\n\n Sorry....Cant run from here!";    
      exit;                                            
     }                                                  
 }


=====
Now here's the working version of the Email article script where I would like the "RETURN TO PREVIOUS PAGE feature added"

#! /usr/local/bin/perl
use CGI;

$q=new CGI;
$friendname=$q->param("friend_name");
$friendmail=$q->param("friend_mail");
$sendername=$q->param("sender_name");
$sendermail=$q->param("sender_mail");
$articlename = "I asked M.D. Sutton to send you this Informative article";

#The article name can be passed as hidden field.
$filePath = "/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt";
# the absolute path to the file.
open (FILE, $filePath) || die "cant open $filePath...$!";
while (<FILE>)
{
  $articlebody .= $_;
}
close (FILE);

$mail_program = "/usr/sbin/sendmail -t";
open (MAIL, "|$mail_program") || die ("Could Not Open Mail Program");
print MAIL "To: $friendname <$friendmail>\n";
print MAIL "From: $sendername <$sendermail>\n";
print MAIL "Subject: $articlename\n\n";
print MAIL $articlebody;
close (MAIL);

print "Content-type: text/html\n\n";
open (HTML_FILE, "/mnt/web/guide/mdsutton/cgibin/email_articles/success_msg.html") || die "cant open the html file";
@Success_Html = <HTML_FILE>;
close(HTML_FILE);

foreach (@Success_Html)
{
  print $_;
}
;



0
Comment
Question by:Rowby Goren
  • 7
  • 4
12 Comments
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2665274
Adjusted points from 100 to 250
0
 
LVL 1

Expert Comment

by:lambda
ID: 2668061
Sorry, rowby I could not access this site at all yesterday, and I couldn't read your comments to the previous question. So I saw this question only just now.

 For the "return to previous page" feature, do a slight addition in your success_msg.html.

 Where you want that message to appear,
give this...

<A HREF="http://www.mdsutton.com/auto-1.html"><img border="0" src="http://www.mdsutton.com/images/click-prev-page.gif" width="192" height="86"></A>

This will take you to www.mdsutton.com/auto-1.html.
Actually I copied it from the link you had given. If you want to go to some other page, say www.yahoo.com, modify the <a href > tag like this...

<A HREF="http://www.yahoo.com"><img border="0" src="http://www.mdsutton.com/images/click-prev-page.gif" width="192" height="86"></A>

0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2669947
Not quite what I had in mind.

If you look at the code (and/or visit the web site link, you'll see that no matter what page the Thank You appears, when the button is pushed, it returns the person to the previous page, --without me having to hand code the return URL for the many different pages that will have this form on this site.  

I don't know anything about cgi, but here is a snippet of the above code I posted which perhaps will give you a clue to the solution:

;
   $return_to=$ENV{'HTTP_REFERER'};
   
Somehow the code "remembers" what page/url it came from and "on the fly" inserts the return URL into the code.

Any thoughts?

Rowby
0
 

Expert Comment

by:drapoc
ID: 2670929
rowby,

below are the pieces that you need to place in your cgi script to get the button to work correctly. Except that you are not actually printing html like the first example you have. You are printing out a pre-generated thank you page.

I cannot tell exactly where that pregenerated page stops.

Does it display the return button?
If it does not then the end of your code would look like this:

foreach (@Success_Html)
{
  print $_;
}
 
<A HREF="$return_to"><img border="0" src="http://www.mdsutton.com/images/click-prev-page.gif" width="192" height="86">

The following should be placed with the variables you have declared at the top.

   $return_to=$ENV{'HTTP_REFERER'};

I hope this helps    
0
 
LVL 1

Expert Comment

by:lambda
ID: 2671780
Another option is using Javascript if you just want to go to the previous page... like this:

<a href="javascript:history.go(-1)"><img border="0" src="http://www.mdsutton.com/images/click-prev-page.gif" width="192" height="86"></a>

But for this, the browser has to be Javascript enabled.
0
 
LVL 1

Expert Comment

by:lambda
ID: 2671787
For using the HTTP_REFERER, you can do this:

In your success_msg.html, use the following tags where you want the "previous page" button to appear:

<a href="PREV_PAGE_LINK"><img border="0" src="http://www.mdsutton.com/images/click-prev-page.gif" width="192" height="86"></a>


And in the Perl program, do this:

$return_to = $ENV{'HTTP_REFERER'};
foreach (@Success_Html)
{
  s/PREV_PAGE_LINK/$return_to/g;
  print $_;
}

That should do.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 9

Author Comment

by:Rowby Goren
ID: 2673667
Is this correct?  Among other things, note the line that I modified to read:

foreach (@success_msg.html)




#! /usr/local/bin/perl
use CGI;

$q=new CGI;
$friendname=$q->param("friend_name");
$friendmail=$q->param("friend_mail");
$sendername=$q->param("sender_name");
$sendermail=$q->param("sender_mail");
$return_to=$ENV{'HTTP_REFERER'};  
$articlename = "I asked M.D. Sutton to send you this Informative article";

#The article name can be passed as hidden field.
$filePath = "/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt";
# the absolute path to the file.
open (FILE, $filePath) || die "cant open $filePath...$!";
while (<FILE>)
{
  $articlebody .= $_;
}
close (FILE);

$mail_program = "/usr/sbin/sendmail -t";
open (MAIL, "|$mail_program") || die ("Could Not Open Mail Program");
print MAIL "To: $friendname <$friendmail>\n";
print MAIL "From: $sendername <$sendermail>\n";
print MAIL "Subject: $articlename\n\n";
print MAIL $articlebody;
close (MAIL);

print "Content-type: text/html\n\n";
open (HTML_FILE, "/mnt/web/guide/mdsutton/cgibin/email_articles/success_msg.html") || die "cant open the html file";
$return_to = $ENV{'HTTP_REFERER'};
foreach (@success_msg.html)
{
  s/PREV_PAGE_LINK/$return_to/g;
  print $_;
}
 
;
0
 
LVL 1

Expert Comment

by:lambda
ID: 2675313
print "Content-type: text/html\n\n";
open (HTML_FILE, "/mnt/web/guide/mdsutton/cgibin/email_articles/success_msg.html") || die "cant open the html file";
@Success_Html = <HTML_FILE>;
close(HTML_FILE);

$return_to = $ENV{'HTTP_REFERER'};
foreach (@Success_Html)
{
  s/PREV_PAGE_LINK/$return_to/g;
  print $_;
}



U shld have the line with PREV_PAGE_LINK in the success_msg.html also as follows:

<a href="PREV_PAGE_LINK"><img border="0" src="http://www.mdsutton.com/images/click-prev-page.gif" width="192" height="86"></a>

0
 
LVL 1

Expert Comment

by:lambda
ID: 2675329
Oh, one more thing. I think you need to escape the underscores (_)

Change the following line:

  s/PREV_PAGE_LINK/$return_to/g;

to :

  s/PREV\_PAGE\_LINK/$return_to/g;


I will just reproduce the entire code:


#! /usr/local/bin/perl
use CGI;

$q=new CGI;
$friendname=$q->param("friend_name");
$friendmail=$q->param("friend_mail");
$sendername=$q->param("sender_name");
$sendermail=$q->param("sender_mail");
$return_to=$ENV{'HTTP_REFERER'};  
$articlename = "I asked M.D. Sutton to send you this Informative article";

#The article name can be passed as hidden field.
$filePath = "/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt";
# the absolute path to the file.
open (FILE, $filePath) || die "cant open $filePath...$!";
while (<FILE>)
{
  $articlebody .= $_;
}
close (FILE);

$mail_program = "/usr/sbin/sendmail -t";
open (MAIL, "|$mail_program") || die ("Could Not Open Mail Program");
print MAIL "To: $friendname <$friendmail>\n";
print MAIL "From: $sendername <$sendermail>\n";
print MAIL "Subject: $articlename\n\n";
print MAIL $articlebody;
close (MAIL);

print "Content-type: text/html\n\n";
open (HTML_FILE, "/mnt/web/guide/mdsutton/cgibin/email_articles/success_msg.html") || die "cant open the html file";

foreach (@Success_Html)
{
  s/PREV\_PAGE\_LINK/$return_to/g;
  print $_;
}



Do try this and tell me.
0
 
LVL 1

Accepted Solution

by:
lambda earned 250 total points
ID: 2675334
Sorry, rowby, my mind is elsewhere I am making the same mistake again & again.

Here is the correct code once again...


#! /usr/local/bin/perl
use CGI;

$q=new CGI;
$friendname=$q->param("friend_name");
$friendmail=$q->param("friend_mail");
$sendername=$q->param("sender_name");
$sendermail=$q->param("sender_mail");
$return_to=$ENV{'HTTP_REFERER'};    
$articlename = "I asked M.D. Sutton to send you this Informative article";

#The article name can be passed as hidden field.
$filePath = "/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt";
# the absolute path to the file.
open (FILE, $filePath) || die "cant open $filePath...$!";
while (<FILE>)
{
  $articlebody .= $_;
}
close (FILE);

$mail_program = "/usr/sbin/sendmail -t";
open (MAIL, "|$mail_program") || die ("Could Not Open Mail Program");
print MAIL "To: $friendname <$friendmail>\n";
print MAIL "From: $sendername <$sendermail>\n";
print MAIL "Subject: $articlename\n\n";
print MAIL $articlebody;
close (MAIL);

print "Content-type: text/html\n\n";
open (HTML_FILE, "/mnt/web/guide/mdsutton/cgibin/email_articles/success_msg.html") || die "cant open the html file";
@Success_Html = <HTML_FILE>;
close(HTML_FILE);

foreach (@Success_Html)
{
  s/PREV\_PAGE\_LINK/$return_to/g;
  print $_;
}


Hope there are no more problems ...
Sorry again! Clumsy of me.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2681950
Thanks!  It works beautifully!

YOu are making me look like a genius -- fortunately my client never heard of Experts Exchange!

Rowby
0
 
LVL 1

Expert Comment

by:lambda
ID: 2682969
Thanks rowby.
Glad u're happy.

:o)

£.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now