469,289 Members | 2,205 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

Dynamic variables

Kelicula
176 Expert 100+
I am trying to set a variable to one of two things depending on if the client has cookies enabled. I have decided NOT to have a prompt saying "cookies must be enabled" if they don't have cookies enabled, I am going to silently use their IP instead.

The cookie has been added already in the calling page with javascript, with no tests done to see if it actually got written.

But I have also recorded the ip in a hidden form field called "ip".

I need to test if the cookie "id" exists, to decide what my scalar "$id" will contain.


Will this work?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI qw(:standard);
  7.  
  8. my $ip = param('ip');
  9. my $cook = cookie('id');
  10.  
  11. my $id = (defined($cook))? $cook : $ip;
  12.  
  13.  
Thanks!
Oct 8 '07 #1
3 1947
numberwhun
3,503 Expert Mod 2GB
I am trying to set a variable to one of two things depending on if the client has cookies enabled. I have decided NOT to have a prompt saying "cookies must be enabled" if they don't have cookies enabled, I am going to silently use their IP instead.

The cookie has been added already in the calling page with javascript, with no tests done to see if it actually got written.

But I have also recorded the ip in a hidden form field called "ip".

I need to test if the cookie "id" exists, to decide what my scalar "$id" will contain.


Will this work?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI qw(:standard);
  7.  
  8. my $ip = param('ip');
  9. my $cook = cookie('id');
  10.  
  11. my $id = (defined($cook))? $cook : $ip;
  12.  
  13.  
Thanks!
While not having much experience with cookies, I did some searching of CPAN and found a couple of things.

First is HTTP::Cookies::Find. This is a module to find cookies on a machine for the present user. This may help you locate cookies.

Second, if that doesn't help, you could always take the Javascript out of the way and set the cookie(s) with Perl so you know what's going on (and can actually log the success and failure of it, unlike the Javascript routines). That other module is called HTTP::Cookies.

Hope this helped in some way.

Regards,

Jeff
Oct 8 '07 #2
Kelicula
176 Expert 100+
While not having much experience with cookies, I did some searching of CPAN and found a couple of things.

First is HTTP::Cookies::Find. This is a module to find cookies on a machine for the present user. This may help you locate cookies.

Second, if that doesn't help, you could always take the Javascript out of the way and set the cookie(s) with Perl so you know what's going on (and can actually log the success and failure of it, unlike the Javascript routines). That other module is called HTTP::Cookies.

Hope this helped in some way.

Regards,

Jeff

Thanks, Jeff!

That sounds a lot better. The only reason I was using javascript was because the referring page is a "static" document with no perl in it.
But I am in the process of switching everything to a Embperl dynamic site, and
should probably generate that page with cgi. It will eliminate a lot of these kinda troubles.

Do you know if that statement:
Expand|Select|Wrap|Line Numbers
  1.  
  2. my $id = (defined($cook)? $cook : $ id;
  3.  
  4.  
will work?

I have never used the "defined" function before.
Oct 8 '07 #3
numberwhun
3,503 Expert Mod 2GB
Thanks, Jeff!

That sounds a lot better. The only reason I was using javascript was because the referring page is a "static" document with no perl in it.
But I am in the process of switching everything to a Embperl dynamic site, and
should probably generate that page with cgi. It will eliminate a lot of these kinda troubles.

Do you know if that statement:
Expand|Select|Wrap|Line Numbers
  1.  
  2. my $id = (defined($cook)? $cook : $ id;
  3.  
  4.  
will work?

I have never used the "defined" function before.
From what I can see in the documentation of the defined function, it looks like it should. I guess the only way would be to test it out.

Regards,

Jeff
Oct 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Tommy Lang | last post: by
1 post views Thread by Tommy Lang | last post: by
1 post views Thread by Nathan Bloomfield | last post: by
12 posts views Thread by scott | last post: by
2 posts views Thread by deejayquai | last post: by
2 posts views Thread by JWL | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.