Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts

    session variables

    I need help setting a session variable. I can usually get things to work by reading and a lot of trial and error, but session variables has got me very lost. In a php script I constructed a simple site counter that tested for the existence of a variable and if not set it added one to the counter value. I then set the variable as the IP address. As the user viewed various pages, the counter would no longer increment, because the variable was now set. In php this is easy with the $_SESSION['variable']. But in Perl I can not figure this out.

    Can some one point me in the direction of a good tutorial on this or better provide me with some sample code? Thanks.

  • #2
    New Coder
    Join Date
    Mar 2010
    Location
    New York
    Posts
    29
    Thanks
    0
    Thanked 1 Time in 1 Post
    I'm pretty sure that in Perl all you need to do is type this:
    Code:
     $session = new CGI::Session();
         #save the IP somehow...
    $session{"IP_ADDRESS"}   =        $q->param("$IPaddress");
    I don't know if that helps. That's what I've always used for session storage, but I don't really know a lot about Perl.

  • #3
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the response. When I use your code I get this error message:

    Can't call method "param" on an undefined value at ...
    The code that I have is:

    Code:
    $_start=$ENV{'REMOTE_ADDR'};
    $session = new CGI::Session();
    $session{"IP_ADDRESS"} = $q->param("$_start");
    I tried entering my $_start above these lines but that did not stop the error message.

    I am looking for suggestions.

  • #4
    New Coder
    Join Date
    Mar 2010
    Location
    New York
    Posts
    29
    Thanks
    0
    Thanked 1 Time in 1 Post
    (facepalm)
    I'm not really great with Perl, but don't you need to actually give it a parameter?
    (and you can read that in an incredibly sarcastic tone of voice)

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Hi,

    You might fins that there is some confusion caused by the naming of your variable. $_ is a default perl variable so I never start a var name with _

    Better, I think to use $start or in this case, $ip_address

    Code:
      use CGI;
      use CGI::Session;
      use strict;
      use CGI::Carp qw(fatalsToBrowser);
    
    
      my $cgi       = CGI->new;
      my $session = new CGI::Session($cgi) or die CGI::Session->errstr;
    
      $session->param('ipAddress', $ENV{REMOTE_ADDR});
    
      my $ip_address = $session->param('ip_address');
    Doing it that way, you store the ip_address and cal it in for use in the script, all in one go.

    The longer way to do it could be:

    Code:
      my $session = new CGI::Session($cgi) or die CGI::Session->errstr;
    
      my $ip_address = $ENV{REMOTE_ADDR};
    
      $session->param('ipAddress', $ip_address);
    
      $ip_address = $session->param('ip_address');
    hth

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #6
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks bazz

    I took your suggestions and modified my code and the error has ceased, however, the variable does not behave as I expected a session variable to behave. Below is the relevent part of my code. Here the session variable is set once the counter has incremented when the user first visits the site. As the vistor views other pages and returns to the home page the session variable should be set and therefore no further increments in the counter. But that is not working. If I move code so the session variable is set before the counter increment, the count does not increment.

    I also tried echoing the session variable in other pages to test whether it truly is a session variable and that does not work either.

    I have wondered if SESSIONS is installed, but I assume that an error would have occurred when I stated - use CGI::Sessions, if it were not available.

    Any additional advice?

    Code:
    use CGI; 
    use CGI::Session;
    use strict;
    use CGI::Carp qw(fatalsToBrowser);

    Code:
    my $val;
    my $count;
    my @data;
    my $ipaddress;
    my $session;
    my $cgi;
    
    
    open(COUNTER,"$log")or die;
    @data=<COUNTER>;
    close(COUNTER); 
    foreach $val (@data)
    {
     chomp($val);
     ($count)=split(/\|/,$val);
    }
    
    if (!defined($ipaddress)){
    $count = $count + 1;
    }
    open(COUNTER, ">$log")or die;
    print COUNTER $count;
    close(COUNTER); 
    
    $session = new CGI::Session($cgi) or die CGI::Session->errstr;
    $ipaddress=$ENV{'REMOTE_ADDR'};
    $session->param('ipaddress', $ipaddress);
    $ipaddress= $session->param('ipaddress');

  • #7
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Hi Fishhead,

    The code I gave has to be at the top of your scripts - all of them.

    To check that the session is persisting from script to script, you can use this line to show you the session_id,

    print $session->header;

    or add this below the code of my earlier post.

    Code:
    my $session_id = $session->id;
    print qq( sess_id = $session_id );
    That value should be the same on all pages otherwise the session is not being retained and we then need to see why that would be. Have you got an online page to look at?

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #8
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Further to that last post, I am slightly confused with this sentence.

    As the vistor views other pages and returns to the home page the session variable should be set and therefore no further increments in the counter.
    I assume you meant that 'as they view other pages'... the session is set and so there would be no further increments of the counter'. Are you counting the number of home page views or the number of page views in total??

    I think the way you are doing it could be better and it should be done in a mysql table. Get all that data you can and compile the stats you need.

    I can say that because I went along your route in the past and FishMonger coaxed me to MySQL since when, I ain't looked back. But if you are trying solely to learn perl, then that suggestion may not be best for you.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #9
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks again bazz,

    My goal was to count the total number of visits to the site, regardless of how many pages the user may view. A view of the home page is one visit. A person viewing 8 pages is still only one visit. Right now the counter is clicking on each page.

    As I mentioned in my first post, I was able to accomplish this in php using a session variable which was set following the counter increment on the first visit to the home page. But perl is new to me and similar but different than php.

    I am in a series of meetings this weekend and through most of next week, so I am visiting my coding during breaks or when the discussion is to boring. I will try inserting your code at the top of each .pl file and see if that solves my current problem.

    Fishhead

  • #10
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    bazz

    I am feeling a bit on the slow side with this aspect of perl. My efforts to establish a session variable are failing. When I paste the code into each file, the variable changes when each page is viewed and the counter keeps increasing. If I do not place the code in each page the counter keeps incrementing by one for each page.

    As I have stated before, I have done very little with perl programing todate. But the input, lay out, booleen stuff, has been fairly straight forward. Storing a variable for use through a session is becoming evasive. Why I do not know, but it just is not working for me.

    The counter that I wish to create is certainly not a deal breaker, but it bugging me that I can not figure this out. I must be making more difficult than it truly is.

    Fishhead

  • #11
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Your script has several problems.

    First, you should use file locking on your counter file. We can that locking code to what you have, however, a better approach would be to use the File::CounterFile module, which handles everything for you.

    You have a discrepancy in data format of your counter file. The first time you access it (for reading), you split the line by the | character, so presumably that line has multiple values/items. However, when you write to the file, you only output a single integer.

    Incrementing the counter needs to be in a conditional that checks the 'ipaddress' session param.

    Try this example.

    page1.pl
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Session;
    use CGI::Carp qw(fatalsToBrowser);
    use File::CounterFile;
    use Data::Dumper;
    
    my $cgi     = CGI->new;
    my $session = CGI::Session->new;
    my $counter = File::CounterFile->new("./COUNTER", 0);
    my $visits  = $counter->value;
    
    if ( !$session->param('ipaddress') ) {
        $visits  = $counter->inc;
        $session->param('ipaddress', $ENV{'REMOTE_ADDR'});
    }
    
    print $session->header,
          $cgi->start_html('page1');
    
    print $cgi->h1($session->param('ipaddress').
                   " has visited this site $visits time(s)"),
          $cgi->a({href=>"/cgi-bin/page2.pl"}, 'Next Page');
    
    #print '<pre>', Dumper($session), '</pre>';
    print $cgi->end_html;
    page2.pl
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Session;
    use CGI::Carp qw(fatalsToBrowser);
    use File::CounterFile;
    use Data::Dumper;
    
    my $cgi     = CGI->new;
    my $session = CGI::Session->new;
    my $counter = File::CounterFile->new("./COUNTER", 0);
    my $visits  = $counter->value;
    
    if ( !$session->param('ipaddress') ) {
        $visits  = $counter->inc;
        $session->param('ipaddress', $ENV{'REMOTE_ADDR'});
    }
    
    print $session->header,
          $cgi->start_html('page2');
    
    print $cgi->h1($session->param('ipaddress').
                   " has visited this site $visits time(s)"),
          $cgi->a({href=>"/cgi-bin/page1.pl"}, 'Previous Page');
    
    #print '<pre>', Dumper($session), '</pre>';
    print $cgi->end_html;

  • #12
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    FishMonger

    Thanks for your help. The script for this website is located on a remote server where space is bought. I am receiving the following error message from using your code. I can contact the server company and request that modules are added, but I do not have a clue whether they will give a damn.

    Can't locate File/CounterFile.pm in @INC ...
    This is a very new world for me. This scipts is stored in cgi-bin and has been and still is a learning experience for me.

    Any suggestions on how to install the needed modules or get around the CounterFile modules would be greatly appreciated.

    Fishhead

  • #13
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    You can easily install the module yourself into your own directories.

    The CounterFile.pm module is a small pure Perl module that doesn't need compiling. It would be best, but not required, to create a 'MyModules' directory under your cgi-bin dir and under that dir create a 'File' dir and put the CounterFile.pm file in that directory.

    You can download the module from here:
    http://search.cpan.org/~gaas/File-Co...CounterFile.pm

    Once you have it installed, you need to add one more line to the script, prior to loading the CounterFile module.
    Code:
    use lib 'MyModules';
    So, the beginning of your script would look like this:
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Session;
    use CGI::Carp qw(fatalsToBrowser);
    use lib 'MyModules';
    use File::CounterFile;
    That's the manual process, but there are other/better ways.
    http://perldoc.perl.org/perlfaq8.htm...ary-directory?

  • #14
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    FishMonger

    thanks for the help. this could be my bad eyes but I am now getting an error message telling me the module can not be fould.

    Can't locate File/CounterFile.pm in @INC (@INC contains: MyModules /usr/local/lib/perl5/5.6.1/
    I placed CounterFile.pm within cgi-bin/MyModules/File/ and added the use lib 'MyModules'; line in my code.

    Fishhead

  • #15
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Sorry, my fault.

    You need to specify the absolute or relative path in the use lib statement.
    Code:
    use lib './MyModule';


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •