Connecting Tech Pros Worldwide Help | Site Map

Help on Perl and Javascript cookie

rajiv07's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: Chennai
Posts: 141
#1: Dec 19 '07
Hi to all

I have doubt on fetch the cookie in perl which is stored in html using javascript.

For that i am using in Javascript

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var exdate=new Date();
  3. var value="rajiv";
  4.  
  5. exdate.setDate(exdate.getDate()+expiredays);
  6.  
  7. document.cookie=c_name+ "=" +escape(value)+
  8. ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
  9. </script>

cookie.pl
----------

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use CGI;
  4.  
  5. my $cgi=new CGI;
  6.  
  7. print $cgi->header;
  8.  
  9. my $cCookie=$cgi->cookie("c_name");
  10.  
  11. print $cCookie;
But the $cCookie has rerurn nothing.

Is any Idea.

Thank u.

Regards

Rajiv
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#2: Dec 19 '07

re: Help on Perl and Javascript cookie


I would use CGI::Cookie rathe than using the JavaScript to set the cookie. You might also want to use Sessions which would be better than cookies.

--Kevin
rajiv07's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: Chennai
Posts: 141
#3: Dec 20 '07

re: Help on Perl and Javascript cookie


Quote:

Originally Posted by eWish

I would use CGI::Cookie rathe than using the JavaScript to set the cookie. You might also want to use Sessions which would be better than cookies.

--Kevin


Hi

Thanks eWish.But i want to get the cookies which is stored in Javascript(html file).Is any idea to get perl vs Javascript cookies interface.

Thank u.

Regards
Rajiv
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#4: Dec 20 '07

re: Help on Perl and Javascript cookie


Quote:

Originally Posted by rajiv07

Hi

Thanks eWish.But i want to get the cookies which is stored in Javascript(html file).Is any idea to get perl vs Javascript cookies interface.

Thank u.

Regards
Rajiv

I think that is kind of what eWish is saying. Instead of using the Javascript to set your cookie and then trying to read it with Perl, why not just handle it all with Perl and CGI, using the the module that eWish provided?

Regards,

Jeff
rajiv07's Avatar
Familiar Sight
 
Join Date: Jun 2007
Location: Chennai
Posts: 141
#5: Dec 20 '07

re: Help on Perl and Javascript cookie


Quote:

Originally Posted by numberwhun

I think that is kind of what eWish is saying. Instead of using the Javascript to set your cookie and then trying to read it with Perl, why not just handle it all with Perl and CGI, using the the module that eWish provided?

Regards,

Jeff


Hi Jeff

Let me explain what i am trying to do.I have two html file and one pl file.

1, referrer.html
2. index.html
3. referrer.pl

Here the referrer.html links to index.html.In index.html has so many html link to display some other details.But there is one link to connect referrer.pl.Here i want to store the referrer URL in the index.html obviously the referrer will be http:/myhost/referrer.html.Here to store the referrer value i am using javascript cookie in index.html.In referrer.pl i will get the stored cookie value and do some calculation.

For this purpose i want PERL and javascript cookie interface.

Is any idea

Thanks.

Regards

Rajiv.
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#6: Dec 24 '07

re: Help on Perl and Javascript cookie


Can you show an example of how you are storing the data for the cookies with the referrer url?

Also, since referrers can be spoofed then you might want to consider using sessions and which is much safer and has less vulnerabilities.

--Kevin
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#7: Dec 25 '07

re: Help on Perl and Javascript cookie


If you really want to read the cookie written by javascript, all you need is the CGI module and a little snippet of code.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7.  
  8. my $q = new CGI;
  9.  
  10. my $cookie = $q->cookie('theCookieName');
  11.  
  12. # Then if you want to do calculations with the cookie 
  13. #data the value of the cookie is stored in "$cookie" variable.
  14. # CGI also has a redirect function:
  15.  
  16. print $q->redirect(-url=>'http://www.someurl.com');
  17.  
  18. # Or you can just print the location header manually.
  19.  
  20. print "Location: http://www.someurl.com\n\n";
  21.  
  22.  
It doesn't matter what you used to write the cookie, if there's a cookie there, and it is set to the proper domain,
IE: you will be reading the cookie from your cgi, or cgi-bin now with the referer.pl so you will want to make sure that it is valid for that domain.

BUT!!!!!!!!

Did I say that loud enough?

You should listen to eWish, and use sessions, it's much better!!
Some browsers are able to modify what is sent as the $ENV{'HTTP_REFERER'}; variable.

CGI 101:
Never trust the user input, or the browser.

Please give us your code, and maybe we can help you a little better....
Reply


Similar Perl bytes