View Full Version : HTTP_REFERER not carrying through form
HI,
I am stuck trying to gather the ENV variables from the sender of a form. I might have to post the code but I am hopeful that if I understand what I am doing wrong, that I may be able to fix it myself.
From the initial page form, clicking preview, takes you to sub preview, The clicking submit there, takes you to the submission and the response page.
In both the form and the preview sub, I have added
<input type="hidden" name="referer" value="$Referer"> and in the sendmail, I have the value $Referer included. However, the message received shows me that I have arrived at the form from within my own site. It does this even if I have arrived at the form through the address bar. :confused:
Is it an easy to diagnose error that I have made or do you need the code.
I think I have found that the env variable is collecting the info each time the script runs so I need help plz (I think) in locking the firstly found variable so that it is held throughout the preview and then submission
Bazz
mlseim
02-14-2005, 07:07 PM
Bazz ...
First of all, I tried this script:
$ref=$ENV{'HTTP_REFERER'};
print "Referrer = $ref\n";
From a link on my site, it came back with my site's name.
From the address bar, it comes back blank (null).
Now, about the form...
I'm not really sure what you're doing ...
Are you trying to capture the user's sitename,
or are you trying to keep anybody from accessing your
programs from their site?
I am trying to capture as many of the env variables from the user as I can. I have someone sending me anonymous messages through the form and I know they know me. So I want to get as much as I can about themselves and their machine so that when they next ask for help, I can check what details are in their PC to be able to surprise them - that they've been sussed.
so really I just want to capture the env variables from entry to the form and then hold them throughout the preview (fixed) so that sendmail sends them to me.
Bazz
mlseim
02-14-2005, 07:42 PM
Below is a Perl snippet that shows you the types of things you can capture.
Copy and Paste the code, give it a name like "user.cgi" upload it and try it.
Now, about gathering information in YOUR script.
You're going to find it really doesn't show anything. A person's IP address
can change each time they connect. They are filling out the form from
your site, so it will always be referred from your site. They can give you
a fake email address, fake name (nothing you can do about that).
The best you can do is to help out the "honest" user that accidently hits
submit without filling out all fields. Before the preview, check for blank
fields or badly formatted email addresses. For the "honest" user that
has cookies enabled, maybe utilize some cookies to keep track of some
things (remember that some users delete their cookies each time).
I do have an "email address" checker (for mistyped addresses like,
bill@aol or bill.aol.com or bill$aol.com)
if you want that snippet, let me know.
==========================================================
#!/usr/bin/perl
$font = "<font size=\"-1\" face=\"Arial\">";
$ip = $ENV{'REMOTE_ADDR'};
$dns_ip = $ip;
$dns_ip =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
$dns_ip = pack('C4', $1, $2, $3, $4);
$hostname = (gethostbyaddr($dns_ip, 2))[0];
if(!$hostname) {
$hostname = "-ip only-";
}
print "Content-type: text/html\n\n";
print qq~
<html><head>
<title>User Info</title>
</head>
<body>
<table align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" colspan="3">
<p><font face="arial" color="#000000" size="-1">
<b> </b><br><br><br><br><br><br><br>
<strong>Perl Gathering Information About You and Your Browser</strong><br><br>
</td></tr>
<tr><td colspan="3" valign="top" height="200"><font face="arial" size="-1">
<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" bgcolor=\"EEEEEE\">
<tr><td bgcolor=\"#DDDDDD\" colspan=\"2\">$font<b>Information About You</b></font></td></tr>
<tr><td align=\"right\">$font IP:</font></td>
<td>$font <b>$ip</b></font></td></tr>
<tr><td align=\"right\">$font Hostname:</font></td>
<td>$font <b>$hostname</b></font></td></tr>
<tr><td align=\"right\">$font Port:</font></td>
<td>$font <b>$ENV{'REMOTE_PORT'}</b></font></td></tr>
<tr><td align=\"right\">$font Request method:</font></td>
<td>$font <b>$ENV{'REQUEST_METHOD'}</b></font></td></tr>
<tr><td bgcolor=\"#DDDDDD\" colspan=\"2\">$font<b>Information About Your Browser</b></font></td></tr>
<tr><td align=\"right\">$font Agent:</font></td>
<td>$font <b>$ENV{'HTTP_USER_AGENT'}</b></font></td></tr>
<tr><td align=\"right\">$font Connection:</font></td>
<td>$font <b>$ENV{'HTTP_CONNECTION'}</b></font></td></tr>
<tr><td align=\"right\">$font Accept:</font></td>
<td>$font <b>$ENV{'HTTP_ACCEPT'}</b></font></td></tr>
<tr><td align=\"right\">$font Accept Languaga:</font></td>
<td>$font <b>$ENV{'HTTP_ACCEPT_LANGUAGE'}</b></font></td></tr>
<tr><td align=\"right\">$font Accept Charset:</font></td>
<td>$font <b>$ENV{'HTTP_ACCEPT_CHARSET'}</b></font></td></tr>
</table>
</td></tr>
<tr>
<td align='center' valign='top' height='250'><a href="javascript:window.close();"><img src='../close.gif' border='0' width='140' height='45' alt='Close This Window'></a></td>
</tr>
</table>
</body>
</html>
~;
Thanks Mlseim for that...
I do have an "email address" checker (for mistyped addresses like,
bill@aol or bill.aol.com or bill$aol.com)
if you want that snippet, let me know.
Yes that would be useful as I have only so far got the normal field checking things done.
I may be wrong but I think that the person I am seeking has a fixed IP by nature of their ISP and their connection. I don't need this script for anything major but it would be nice to catch out those numpties who think they're smart :)
bazz
mlseim
02-14-2005, 08:48 PM
#somewhere in your script, you check the email address
#(whatever variable it happens to be, in this example, it's $sender)
unless(&CheckEmailAddress($sender)) {&no_email;}
#Here are the subroutines that will get pasted on the bottom of your script:
sub CheckEmailAddress() {
# This regexp validates the format of an email address. It returns the cleaned
# version of the email address, or blank if the address was not valid.
#
# An email address must be of the form:
# 1) (trim any spaces or tabs or linefeeds or carriage returns)
# 2) (possibly one quotation mark)
# 3) (one or more characters for username, excepting a quotation mark)
# 4) (possibly one quotation mark)
# 5) @
# 6) (one or more characters for hostname(s), excepting [ <>\t])
# 7) .
# 8) (two or more characters for top-level-domain, excepting [ <>\t])
# 9) (trim any spaces or tabs or linefeeds or carriage returns)
#
# 1 2 3 4 56 7 8 9
# .............'''.......'''.'''''''''..''''''''''''''''''.............
$_[0] =~ /[ |\t|\r|\n]*\"?([^\"]+\"?@[^ <>\t]+\.[^ <>\t][^ <>\t]+)[ |\t|\r|\n]*/;
return $1;
}
sub no_email {
print "Content-type: text/html\n\n";
print "<html><body><center><font size=+1 color=\"FF0000\"><b>Oops, but it looks as if<br>
you may have mis-typed your email address.<br><br>
Please click your back button and see what's wrong.<br><br>
Thank you.
</b></font>";
print "</center></body></html>\n";
exit;
}
THats great mlseim, I'll try to play about with that.
But I'm curious now about the referer thing. When collected as the page first loads, how would I fix it so that it is held throughout the preview and then sent in the sendmail. I just need to know how to make it constant as I have the other bits done.
bazz
mlseim
02-15-2005, 07:32 PM
I would think all the information would be the same from page to page
without carrying it through. The person's IP address won't change, nor
their browser or ISP.
==================================
But to answer your question, you would assign a variable like:
$agent=$ENV{'HTTP_USER_AGENT'};
And, when you create your next form using Perl, have it included
with the HTML ... mixed in with your other fields.
<input type='hidden' name='agent' value='$agent'>
The script that processes the form, will take in the variable $agent,
thus it remains intact through the form.
===================================
If you were simply passing it along from one Perl script to another
Perl script, you would add it on as a variable:
print "Location: http://www.mysite.com/cgi-bin/pscript.pl?agent=$agent\n\n";
.
Thanks, I'll try that again coz I must have done it wrongly before. I saw the lines in your code with input hidden and copied their format, however, I just got the HTTP_REFERER info from the previous time the script was run rather than that of the site they were at just before coming in to my form.
Bazz
OK I have found that I really need this for something useful. When a person comes into 'that' contact form, I want to remember the page they were at just beforehand so that after sending the form, the response page lets them click back to where they were. This isn't as excessive as it may seem since normally the nav menu would be on the same page however, the form will be in differnet sites with different menus and I would like to stll have a 'go back to where you were' link, which I think would have to be based on HTTP_REFERER.
So I tried this
$Referer = $ENV{'HTTP_REFERER'};
$Page = $Referer;
<input type="hidden" name="ref" value="$Page">
It seems that each time the script runs ie, when you click to preview and then click to send, the HTTP_REFERER changes and so, therefore, does the variable $Page. How could I stop this. presumably by fixing the variable but I cant think how.
This idea may not work either but what if I split the HTTP_REFERER and then recompile it?
bazz
mlseim
02-16-2005, 09:08 PM
Bazz ...
How about writing a cookie the first time?
Once it's written, you can make it so it won't change it again.
You just read it whenever you need to.
In fact, you could have several variables saved into cookies.
Once the user closes their browser, the cookies expire and
disappear.
I'll google for that unless you have a good tutorial link?
Thanks
Bazz
mlseim
02-16-2005, 09:18 PM
Bazz....
Download Cookie.lib ... it helps make it pretty easy.
http://www.scriptarchive.com/cookielib.html
upload it into your cgi-bin and CHMOD it to 755.
change the variable: $Cookie_Domain = 'catpin.com';
to your own URL.
leave this variable: $Cookie_Exp_Date = '';
alone ... blank means it expires the cookie when the browser closes.
At the top of EVERY script that uses cookies, put this line:
require 'cookie.lib';
Once you get it uploaded, I'll give you some examples to work with.
Or, you can stumble through the instruction sheet.
mlseim
02-17-2005, 03:51 AM
Bazz ...
Once you start using cookies, you'll go wild. You're going to want
to use them for everything ... a log-in for customers, saving settings
for customers ... list goes on and on.
There's one important thing you always have to remember:
If you set a cookie at the beginning of your script, you cannot
read the same cookie back later in the same execution of the script.
Meaning, when you set the cookie, the script must end before you
can read it back. Once you set the cookie with a script, you can
read it back with any other script whenever you want. You can
set a cookie with Perl, and read it back with Javascript too.
Here's an example of mixing Perl with Javascript: http://www.catpin.com/cook.htm
.
Thank you mlseim. Suddenly my brain feels very small. :D
I'll take a read of that oevr the wekend (if not sooner) and see how I might incorporate it.
Oohhhhh I just realised you posted two links. Cookie.lib sounds very exciting so I might have a better weekend now :)
I really want to get the programs finished by end next week coz I have another program that I need to get done. If you're potentially interested in coding it, I can PM you the details otherwise I'll put it in the works offers forum.
Bazz
mlseim
02-17-2005, 04:22 PM
I'm booked for the week ...
But I check into CodingForums periodically, it gives me a nice
break from the toil of work.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.