Wiggins d'Anconia wrote:
Andrew Gaffney wrote:

I'm creating my own module to handle sessions and other stuff for my site:

<code>
package Skyline;

use Apache::DBI;
use Apache::Cookie;
use Exporter;

@ISA = ('Exporter');
@EXPORT_OK = ('check_login', 'init_db', '$dbh');
@EXPORT = ('check_login', 'init_db', '$dbh');
our $dbh;

sub check_login {
my %cookies = Apache::Cookie->fetch;
return ($cookies{uid}->value, $cookies{ut}->value, $cookies{sid}->value, $cookies{rnd1}->value);
}


sub init_db {
$dbh = DBI->connect("DBI:mysql:database=skyline;host=192.168.254.25", "root", "linux", {'RaiseError' => 1});
}


1;
</code>

In order to test my code, I've written another little test script:

<code>
#!/usr/bin/perl

use CGI;
use Skyline;

my $cgi = new CGI;

my ($uid, $ut, $sid, $rnd1) = check_login();

print $cgi->header;
print "<html><body>sid = $sid, rnd = $rnd1, uid = $uid, ut = $ut</body></html>";
</code>


which give me 'sid = Apache::Cookie=SCALAR(0x872a958), rnd = , uid = Apache::Cookie=SCALAR(0x86b1ce0), ut = Apache::Cookie=SCALAR(0x86c0874)' or a variation with different scalar addresses. What am I doing wrong? I know the cookies exist. I wrote another test script:

So how do you set rnd? I am assuming that is what you are asking about, though the question "What am I doing wrong?" is difficult to answer if we don't know what you are *trying* to do. Have you checked the expiration date of the cookie, and if it has a domain/path restriction? I don't see anything in your code specifically, other than the absence of strict and warnings naturally.

I have a different function that sets all of the above cookies when the user logs in to the site. I was using a sessionid and random number to match up to an entry in my MySQL db with information about the user. The module that I'm writing is to replace 5 or 6 lines in each script to get the cookies and check the session validity.


<code>
#!/usr/bin/perl

use CGI;
use Apache::Cookie;

my $cgi = new CGI;
my %cookies = Apache::Cookie->fetch;

print $cgi->header;
foreach (keys %cookies) {
  $cval = $cookies{$_}->value;
  print "$_ = $cval<p>";
}
</code>

This outputs all 4 cookies and their values. Anyone have any idea what I'm doing wrong?


I assume you aren't calling this the second time after calling a script that sets the cookies. Have you tried adding an exists or definedness in the 'check_login' function to make sure the cookie really is in the hash? How about printing all of your cookies from there?

I figured it out this morning before I went to work. mod_perl had been caching my module even after I changed it. It was updating the cache for my test script, so I didn't think of this. A quick restart of Apache grabbed my new code and everything started working again. Here is the current (working) code:


sub check_login {
  my %cookies = Apache::Cookie->fetch or %cookies = {};
  my @keys = ('uid', 'ut', 'sid', 'rnd');
  # The above is there because 'foreach (keys %c)' below wouldn't work for me. It would
  # just return a scalarized (is that a word?) hash reference instead of the keys
  my %c = {uid => 0, ut => 0, sid => 0, rnd => 0};
  foreach (@keys) {
    if($cookies{$_} ne undef) {
      $c{$_} = $cookies{$_}->value;
    }
  }

  return %c;
}

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to