Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI cookies vs. JS Cookies

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
Hey all,

(almost done... phew, I bet some of you can't wait). I created a JS cookie which I know works. Now I want to retrieve that cookie with PERL. First, is that possible since it was written in JS? Secondly, what is the simplist way do this?

- MT
 
I added the following to the top of my CGI file:

use CGI qw/:standard/;
use CGI::Cookie;

# I need to grab those two cookies that I created earlier
%cookies = fetch CGI:Cookie;
$emailCookie = $cookies('peiEmail')->value;
$ipCookie = $cookies('peiIP')->value;

Yes? No? Maybe?

- MT
 
OK, I can tell you that DOES NOT work. So... um... why?

- MT
 
As long as they're written from the same domain it should be fine, no?

Can we see the JavaScript that creates the cookie?

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
mtorbin, when you tested the above script was it located in the same directory as the HTML page?

Just a thought!

X
 
OK, here's my answers:

1) No. The CGI and PL files are all in /cgi-bin/
2) JS THAT CREATES THE COOKIE (it's a bastardized version of something I found online a long time ago)Both expdate and today are declared elsewhere:

function doesCookieExist(cookieName,cookieValue) {
var todaysDate = today.getTime();

FixCookieDate(expdate);
expdate.setTime(expdate.getTime() + (30 * 24 * 60 * 60 * 1000)); // 30 days from the day of creation

if (GetCookie(cookieName)) {
var currentValue = cookieValue;
SetCookie(cookieName, currentValue, expdate, "/", null, false);
}

else {
var currentValue = cookieValue;
SetCookie(cookieName, currentValue, expdate, "/", null, false);
}
}


function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);

if (endstr == -1) {
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

return unescape(document.cookie.substring(offset, endstr));
}


function FixCookieDate(date) {
var base = new Date(0);
var skew = base.getTime(); // dawn of (Unix) time - should be 0

if (skew > 0) { // Except on the Mac - ahead of its time
date.setTime (date.getTime() - skew);
}
}


function GetCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;

while (i < clen) {
var j = i + alen;

if (document.cookie.substring(i, j) == arg) {
return getCookieVal(j);
}

i = document.cookie.indexOf(" ", i) + 1;

if (i == 0) {
break;
}
}

return null;
}


function SetCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}


function DeleteCookie(name) {
var path = "/";
var domain = null;

if (GetCookie(name)) {
if (confirm("Are you sure you want to delete this cookie?")) {
document.cookie = name + "=" + ((path) ? ": path=" + path : "") + ((domain) ? ": domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
}

Does that help? The error log says syntactical errors on those new lines in the CGI.

- MT
 
The error log says syntactical errors on those new lines in the CGI.

That's where we should start so ... bring it on

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
OK... well, the problem is these three lines:

%cookies = fetch CGI:Cookie;
$emailCookie = $cookies('peiEmail')->value;
$ipCookie = $cookies('peiIP')->value;


Syntactical errors. There is where I need the help.

- MT
 
For starters, shouldn't we replace this:
Code:
%cookies = fetch CGI:Cookie;
with this:
Code:
%cookies = fetch CGI::Cookie;
 
How about this:
Code:
use CGI qw/:standard/;
use CGI::Cookie;

%cookies = fetch CGI::Cookie;
$emailCookie = $cookies{'peiEmail'}->value;
$ipCookie = $cookies{'peiIP'}->value;
print header(-cookie=>[$ipCookie,$emailCookie]);
 
Hi

As first step in debuging I would test if the cookie was setted and transmitted as I want :
Code:
print $ENV{HTTP_COOKIE};

If ok, then comes the all sort of modules.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top