Gossamer Forum
Home : General : Perl Programming :

cookie failure

Quote Reply
cookie failure
I can't send a cookie through an image. Ideas?

I'm calling the script with a <img src= ...>

---------
use CGI;
$query = new CGI;

print "Location:$imageurl\n\n";

$cookie = $query->cookie(-name=>'ID',
-value=>$idout,
-path=>'/');

print $query->header(-cookie=>$cookie);

---------


Quote Reply
Re: cookie failure In reply to
Try this, it works for me!

Code:
#!/usr/local/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use CGI;

my $in = new CGI;
my $name = "cookiename";
#my $name = $ENV{'HTTP_REFERER'};

my $cookie = $in->cookie(
-name => 'cookiename',
-value => $name,
-domain => '.domain.com',
-expires => ' 1h',
-path => '/'
);

print $in->header(
-type => 'image/gif',
-expires => 'now',
-cookie => $cookie
);
open (IMG, "/users/www/data/images/pixel.gif") or die $!;
while (<IMG>) { print; }
close IMG;

__END__
Too bad I can't seem to figure out how make use of $ENV{'HTTP_REFERER'}. This because it displays the name of the website calling the image, and not the referer. Any ideas someone? Otherwise I would have to have some javascript get the referer and write an image like /setcookie.cgi?ref=http://www......bla.bla.html

Quote Reply
Re: cookie failure In reply to
Ok.... for me domain and path options do not work on netscape. This is not a problem since the cookie can work according to the browser but I'm having this problem ... always with netscape :((

-------------- 1 ----------------
$cookie = $query->cookie(-name=>'userID',
-value=>$idout,
-expires=>'+10y');

print $query->header(-cookie=>$cookie);

print "Location:$bannerurl\n\n";

-----------------------------------------
This is sending a cookie but 'print Location' generates an error.

-------------- 2 ----------------
print "Location:$bannerurl\n\n";

$cookie = $query->cookie(-name=>'userID',
-value=>$idout,
-expires=>'+10y');

print $query->header(-cookie=>$cookie);

-----------------------------------------
This is sending the banner correctly but the cookie is not saved.


What do you think?

Thanks.





Quote Reply
Re: cookie failure In reply to
Now I don't understand you anymore.

First you wanted to call the script as an image so I provided a code that would work if you would like to call the script as an image. But now your talking about a redirect. Anyway:
Code:
#!/usr/local/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use CGI;
my $in = new CGI;
my $partner = 'cookievaluetoset';
my $cookie = $in->cookie(
-name => 'partner',
-value => $partner,
-domain => '.domain.com',
-expires => ' 1d',
-path => '/'
);
print $in->redirect(
-url => 'http://www.test.com',
-cookie => $cookie
);
__END__
Notes:
- Option 2 didn't work because you first need to set the type, then the cookie and then the location!
- I you define a path and domain it will work, only you didn't!

Quote Reply
Re: cookie failure In reply to
Ok. .... I'm solving almost everything. Even if I must say that 'path' and 'domain' are creating problems most of the times with netscape. But it works :)

Now ... I'm tring to call banners through a javascript src in order to send more variables to remote pages (if any).
But I'm getting may errors :((

---js code---
<script language=javascript src="http://mydomain/load.cgi?user=test"></script>
-------------

Worst situation given by netscape:

illegal character.
GIF89aÔ<
......^

(I tried to print an 'image/gif' header with no success)

Then MSIE is telling that there is an illegal character in

<script language=javascript src="http://mydomain/load.cgi?user=test"></script>


Thank your for your help.

Quote Reply
Re: cookie failure In reply to
image/gif means it makes an IMAGE, not a .js file ! So you'll need to tell the script to print something like text or .js

Just read the CGI.pm docs! (Google I'm lucky button: CGI.pm)

Till now you have a cookie script that redirects, prints an image. So now find out how to print html/text/js! And it would be nice, if you would post you sollution here for others Wink

Quote Reply
Re: cookie failure In reply to
This works for me!

Code:
use CGI;

my $in = new CGI;

my $uniquestring;
$uniquestring.= (a..z,A..Z,0..9)[int(rand(62))] for (1..20);

my $lang = $in->param('lang') || '';

print $in->header(
-type => 'text/plain',
-expires => '+1h',
);

print qq~
document.write('<img scr="/images/set.cgi\?user=$uniquestring\&lang=$lang" border=0>');
~;
__END__

Quote Reply
Re: cookie failure In reply to
Great :)

Just to complete my nightmare .... :(((

Netscape only:
I cant save cookies.
I'm getting the popup alert telling me that the cookie will be saved but no cookie is sent to the browser.



Quote Reply
Re: cookie failure In reply to
...are you sure; cookies.txt doesn't get updated untill you closed all browerwindows (=end session)