Setting headers

2001-09-14 Thread Rasoul Hajikhani

Hello,
I am using template toolkit to generate dynamic content and I was
wondering if there is any way to set a cookie after returning OK. Here
is what I mean.

1-There are two scripts that check for access privileges of a user. One
sets two cookies and the other (mine) sets three cookies.

2- My script checks for all the cookies and if it does not find the
third one it should set it. But the catch is here:

package some_package;

# apache packages 

use vars qw($r);

sub handler{

$r = Apache::Request-new(shift);
#code here
$object-{is_allowed_todo} = \is_allowed_todo;

$template = Template-new();
$remplate-process($file,$object);
return OK;
}

sub is_allowed_todo
{
my ($args)  = shfit;
# args are the arguments that I get from my .html file
# [% is_allowed_todo('A HREF=' %]

my $status  = __PACKAGE__-SUPER::is_allowed_todo($r);

if ($status)
{
# do this
}
else
{
# do that
}
}
1;

# The SUPER

package some_other;

sub is_allowed_todo
{
my $r   = shift;

my $cookies = Apache::Cookie-new($r)-parse();
my @webaccess = $cookies-{'webaccess'} ?
$cookies-{'webaccess'}-value() : ();

unless ($webaccess[MIS_APPS::RHS::Constants::ONE()]);
{
my $user=
$cookies-{'webuname'}-value();
my $job = $cookies-{'webjob'}-value();

($code,$status,$dept)   = get_userinfo($user,$job);

Apache::Cookie-new(  $r,
-name   = 'webaccess',
-value  = {
'department'  
= $dept,
'access'  
= $status,
   },
-domain = '.rhythm.com',
-expires= '+24h',
-path   = '/'
  )-bake;
}
return $status;
}

But my problem is that I have already returned OK in my handler. How can
I set another cookie when I have already opened the data stream to the
client browser? I hope I have been clear... :)
Any comments will be greatly appreciated.
-r



Re: Setting headers

2001-09-14 Thread Stas Bekman

On Fri, 14 Sep 2001, Rasoul Hajikhani wrote:

 Hello,
 I am using template toolkit to generate dynamic content and I was
 wondering if there is any way to set a cookie after returning OK. Here
 is what I mean.

 1-There are two scripts that check for access privileges of a user. One
 sets two cookies and the other (mine) sets three cookies.

 2- My script checks for all the cookies and if it does not find the
 third one it should set it. But the catch is here:

The problem is that you don't show where is_allowed_todo gets called. I
guess in the template as in your comment. The solution is very simple.
Change is_allowed_todo to change status in $object or elsewhere. Now
change the handler to do;

  my $status = exists $object-{status}
? $object-{status}
: OK;
  return $status;

does that solve your problem?

You return your status as the last thing in the handler, after the
template was processed, I don't see how can you have this problem

Even better solution would be to run is_allowed_todo from your code and
not template! Run is_allowed_todo, get the status and pass it to your
template as a variable.

One should avoid running code from the templates, as they are designed for
presentation not application logic.

 package some_package;

 # apache packages

 use vars qw($r);

 sub handler{

   $r = Apache::Request-new(shift);
   #code here
   $object-{is_allowed_todo} = \is_allowed_todo;

   $template = Template-new();
   $remplate-process($file,$object);
   return OK;
 }

 sub is_allowed_todo
 {
   my ($args)  = shfit;
   # args are the arguments that I get from my .html file
   # [% is_allowed_todo('A HREF=' %]

   my $status  = __PACKAGE__-SUPER::is_allowed_todo($r);

   if ($status)
   {
   # do this
   }
   else
   {
   # do that
   }
 }
 1;

 # The SUPER

 package some_other;

 sub is_allowed_todo
 {
   my $r   = shift;

   my $cookies = Apache::Cookie-new($r)-parse();
   my @webaccess = $cookies-{'webaccess'} ?
 $cookies-{'webaccess'}-value() : ();

 unless ($webaccess[MIS_APPS::RHS::Constants::ONE()]);
 {
 my $user=
 $cookies-{'webuname'}-value();
 my $job = $cookies-{'webjob'}-value();

 ($code,$status,$dept)   = get_userinfo($user,$job);

 Apache::Cookie-new(  $r,
 -name   = 'webaccess',
 -value  = {
 'department'
 = $dept,
 'access'
 = $status,
},
 -domain = '.rhythm.com',
 -expires= '+24h',
 -path   = '/'
   )-bake;
 }
   return $status;
 }

 But my problem is that I have already returned OK in my handler. How can
 I set another cookie when I have already opened the data stream to the
 client browser? I hope I have been clear... :)
 Any comments will be greatly appreciated.
 -r




_
Stas Bekman  JAm_pH --   Just Another mod_perl Hacker
http://stason.org/   mod_perl Guide  http://perl.apache.org/guide
mailto:[EMAIL PROTECTED]   http://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/