You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Doug MacEachern <do...@pobox.com> on 2000/03/04 03:15:22 UTC

Re: problem with example 4-9 eagle book

On 21 Jan 2000, Fernando Rowies wrote:

> someone can help me with those problems?
> 
> first: trying example 4-9 from eagle book the result is an
> unvarying behavior. Running for first time it display the
> banner randomly, then, when i reload the page, take another
> randomly, but the third or fourth reload, no more 
> pick_randomly_the_image, it display the last, the last,
> last, last, last... what happen?

your browser is probably caching the image from the redirect.
try the version below.  if it still doesn't work, try adding this before
the call to $r->send_http_header:

    my $headers = $r->headers_out;
    $headers->{'Pragma'} = $headers->{'Cache-control'} = 'no-cache';
    $r->no_cache(1);

(you only need to call $r->no_cache with 1.22-tobe)

package Apache::RandPicture2;

use strict;
use Apache::Constants qw(:common REDIRECT);
use DirHandle ();

sub handler {
    my $r = shift;
    my $dir_uri = $r->dir_config('PictureDir');
    unless ($dir_uri) {
	$r->log_reason("No PictureDir configured");
	return SERVER_ERROR;
    }
    $dir_uri .= "/" unless $dir_uri =~ m:/$:;

    my $subr = $r->lookup_uri($dir_uri);
    my $dir = $subr->filename;
    # Get list of images in the directory.
    my $dh = DirHandle->new($dir);
    unless ($dh) {
	$r->log_error("Can't read directory $dir: $!");
	return SERVER_ERROR;
    }

    my @robjs;
    for my $entry ($dh->read) {
	# get the file's MIME type
	my $rr = $subr->lookup_uri($entry);
	my $type = $rr->content_type || '';
	next unless $type =~ m:^image/:;
	push @robjs, $rr;
    }
    $dh->close;
    unless (@robjs) {
	$r->log_error("No image files in directory");
	return SERVER_ERROR;
    }

    my $lucky_one = $robjs[rand @robjs];
    $r->send_http_header($lucky_one->content_type);
    $lucky_one->run;
}

1;
__END__