homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

Changing paths to referal?
Should be simple but I keep getting errors...
JAB Creations

 2:21 pm on Mar 10, 2006 (gmt 0)

This is from Matt Wright's guestbook script...and all I want to do is change these paths to be the referer...thats it.

I tried adding $ENV{'HTTP_REFERER'} and no matter how I put that in I'd get an error.

$guestbookurl = "http://www.example.com/themes/includes-toolbar-comments.php";
$guestbookreal = "/home/example/public_html/themes/includes-toolbar-comments.php";
$guestlog = "/home/example/public_html/home/themes/includes-toolbar-comments.php";
$cgiurl = "http://www.example.com/cgi-bin/comments.pl";




 7:08 pm on Mar 10, 2006 (gmt 0)

are we supposed to guess what the error is? ;)

JAB Creations

 7:35 pm on Mar 10, 2006 (gmt 0)

Your guess is as good as mine. :-D It's what any other messed up perl script error is like, error 500.

I'm not so much concerned with the error as just knowing how to implement the referer instead of a static url though. Unlike PHP perl doesn't give anything useful to go off of for messages.



 8:47 pm on Mar 10, 2006 (gmt 0)

show how you are trying to add the variable, and you can try adding:

use CGI::Carp qw/fatalsToBrowser/;

at the beginning of the script somewhere (just under the first line should be OK) and it might echo back a more useful error messge.

JAB Creations

 3:53 am on Mar 11, 2006 (gmt 0)

Geez, would have been nice to have had that line of code for debugging a long time ago!

Anyway this works...

$guestbookurl = $ENV{'HTTP_REFERER'};

So the problem is that the absolute server path will not work...so I need to somehow echo just the local path...


...that will echo the server path as so...


...instead of the internet path.

The error was (of course) that the file could not be found.



PS - Where could I find a full list of Perl's defined $ENV{} variables?

I'll need the variable that I can echo... /home/example/public_html/

...that or the full path minus the file name...and then add the file name with another variable.

Remember, I want to make this script dynamic in the sense that it writes to the refering file! :)



 4:10 am on Mar 11, 2006 (gmt 0)

unfortunately you can't rely on the %ENV variables to do what you want. Browsers and firewalls can block some of the information from ever getting to a script, and the server can also be setup to show some and not others, but you can run this code to see what your server returns for %ENV:

use strict;
print "Content-type: text/html\n\n";

foreach my $var (sort keys %ENV) {
print "<b>$var</b> = $ENV{$var} <br>\n";

you could have found the error in the server error logs too, but the line I gave you makes it easy to see what the error is when you run it from a browser.

JAB Creations

 4:30 am on Mar 11, 2006 (gmt 0)

I did some research on the variables; MWHAHA! I have an idea! (and me having an idea is never new)...

Here is what I get with two server enviormentals that catch my eye...

$ENV{DOCUMENT_ROOT} = /home/example/public_html

$ENV{'HTTP_REFERER'} = http://www.example.com/comments/comments.php

Since I can't directly get the server path of the referal file, I'm sure I could take the values that I need and merge them in to a single variable. Then I could just put that variable in to the place of the static server path and success! Or at least thats what I'm hoping...

I really don't know enough about Perl/CGI so I'll give you the visual I can work best with in my head...

$ENV{DOCUMENT_ROOT} = /home/example/public_html

Above is good and nothing we don't want so let's assign it's value to $1.

( so $1 = /home/example/public_html )

Now the tricky part (for me at least) is getting rid of part of the next value of the predefined variable...

$ENV{'HTTP_REFERER'} = http://www.example.com/comments/comments.php

We want to get rid of "http://www.example.com" and assign $2 to equal "/comments/comments.php".

( so $1 = /home/example/public_html )

$2 = $ENV{'HTTP_REFERER'} - http://www.example.com
( so $2 = /comments/comments.php )

That way we can merge $1 + $2....

$3 = $1 + $2

Which I would imagine would equal...


So how do we make that work (if it will?)



 6:18 am on Mar 11, 2006 (gmt 0)

I don't understand what you are using the HTTP_REFERER variable for. Why don't you just do this:

$guestbookurl = $ENV{'DOCUMENT_R0OT'} . '/comments/comments.php';

JAB Creations

 7:13 am on Mar 11, 2006 (gmt 0)

Static Value

Dynamic Value

I need to merge these. Remember, I want to make the script usable from any file on my server that refers to it.


But the script requires the full server path. So in essence we're trying to make the file dynamically create the full server path of the referer.

So any of these examples as referers (as being the form that is submitted to the script)...


Would be reformed in to absolute server paths...


So if you go back and reread my previous you can maybe now with a clearer understanding (because I suck at programming and thus explaining my ideas a lot) that I was trying to construct an -absolute server path of the refering file-.

So in review of the full script (to try and help round out the explenation....

1.) User fills form on ...

2.) Script gets file with referer sent by the browser.

3.) Script uses a miniscript (the script we're trying to put together in this thread) to create an -absolute server referer path-.

4.) The absolute server referer path is set to a variable (in my previous post, this variable was $3).

5.) Later in the script when it writes the contents to the referer we get the $3 variable which is our dynamically created -absolute server referer path-.

The script writes to the file that the form was on.

So...here was my idea slimmed down to a simple question...

How can I set the $2 variable to equal the same as $ENV{'HTTP_REFERER'} but without "http://www.example.com"?

We start with...
$ENV{'HTTP_REFERER'} = http://www.example.com/comments/comments.php

(remember I'm using the specific file as a static referer here, but it would be whatever the refer is in the script)

Then I want to merge...
and ...

and assign these combined variables to $3.

So in this example...
$3 = /home/example/public_html/home/comments.php

So we're removing the domain and then setting the rest of $ENV{'HTTP_REFERER'} to $2.

We then just add $1 and $2 together to set $3.

$3 = whatever the refer was...it's absolute server path.

But I don't know the syntax to the extent you do.



 5:32 pm on Mar 11, 2006 (gmt 0)

hmm, well there is probably a better way to do all this, but this might get you what you want or close to it:

my $domain = 'http://www.example.com';
my $referer = $ENV{HTTP_REFERER};
my $root = $ENV{DOCUMENT_ROOT};
$referer =~ s/$domain//;
$root .= $referer;
print $root;

JAB Creations

 1:27 pm on Mar 12, 2006 (gmt 0)

Got everything working, thanks a ton perl_diver!



 12:28 am on Mar 13, 2006 (gmt 0)

you're welcome :)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved