Forum Moderators: phranque

Message Too Old, No Replies

environment variables - for experts

setting Environment Variables to specific Request headers

         

theRatWonder

9:12 pm on Apr 15, 2007 (gmt 0)

10+ Year Member



Basically, could you tell me how to set a new environment variable from an HTTP request header, or look below and tell me if I'm using "PassEnv" wrongly.

Details:
-------

The problem is, I want to set a variable with a perl script, redirect the page elsewhere and have the variable be able to be picked up at the other end.

Obviously environment variables are only for that sessions so setting an environment variable doesn't work.

You would think that the apache "PassEnv" command would be specifically for this, but I tried this and it doesn't work after I set an environment variable in perl (those not fluent in perl just take my word for it, this should work and doesn't:

------ start.cgi -----

#!/usr/bin/perl
use CGI;
$ENV{'myvar'} = 'hello world!';
print CGI->redirect('end.cgi');
exit;

----------------------

------ .htaccess -----

PassEnv myvar

----------------------

------ end.cgi -------

#!/usr/bin/perl
use CGI;
print CGI->header();
if($ENV{'myvar'}) {print "success!";}
else {print "failure :(";}
exit;

----------------------

Running start.cgi in a browser produces "failure :(".

So the other idea I had was to send it in the header, and then retrieve it from the header at the other end:

----- start.cgi -----

#!/usr/bin/perl
use CGI;
print CGI->redirect(
-uri=>'end.cgi',
-myvar=>'hello world!'
);
exit;

---------------------

------ .htaccess ----

# I was hoping I could somehow use SetEnv to set the variable from the http header
SetEnv myvar #*$!xx

---------------------

end.cgi would be the same.

Please let me know if you can help.
Thanks.
Robin.

[edited by: theRatWonder at 9:26 pm (utc) on April 15, 2007]

theRatWonder

9:25 pm on Apr 15, 2007 (gmt 0)

10+ Year Member



By the way I can prove (and have tested) that the environment variable setting and retrieving in my scripts above is correct as follows:

----- start.cgi ----

#!/usr/bin/perl
use CGI;
$ENV{'myvar'} = 'hello world!';
print CGI->header();
print `perl end.cgi`;
exit;

--------------------

----- end.cgi ------

#!/usr/bin/perl
if($ENV{'myvar'}) {print "success!<br />" . $ENV{'myvar'};}
else {print "failure :(";}
exit;

--------------------

Running start.cgi in a browser prints out:

Success!
hello world!

[edited by: theRatWonder at 9:26 pm (utc) on April 15, 2007]

jdMorgan

9:35 pm on Apr 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The simplest way to pass info from one HTTP transaction to a subsequent one is to set a cookie. Because a 301/302 redirect server response terminates the current HTTP transaction, and because it is up to the client to issue another request (starting a new HTTP transaction) using the URL provided in that redirect response, the choices are to either use a cookie or to redirect to a URL carrying the information you wish to pass as an attached querystring. These two methods are supported by standard browser clients without any customization.

So, either set the cookie response header with a valid cookie containing the data you want to pass, or bolt the data onto the redirect-to URL as in "Location: [exmple.com...]

Jim

theRatWonder

11:06 pm on Apr 15, 2007 (gmt 0)

10+ Year Member



The problem is that I'm actually trying to pick up the variable in a server-parsed page:

------ page.html ----
<html>
<body>
<!--#echo var="myvar" -->
</body>
</html>
---------------------

There is no way to easily pick up a cookie value in SSI is there?

jdMorgan

2:00 pm on Apr 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what you mean, but this works:

------ page.shtml ----

 <html>
<body>
<!--#echo var="[b]HTTP_COOKIE[/b]" -->
</body>
</html>

---------------------

Jim