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.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP session variables in Perl

    Hey,

    Hi, I need to access existing PHP session variables(actually just one...) in a perl script?

    I tried installing PHP::Session and then trying several variations of

    my $session= PHP::Session->new($id);
    my $id = $session->id;
    my $login = $session->get('username');

    But obviously that doesn't work. I hope I posted this in the right spot. I would really appreciate any and all help ASAP.

    Many, Many Thanks in advance.

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    OK, until an worthy expert arrives I will suggest this: -

    The session data (as required), is stored in the browser so it does not matter if it is perl or php.

    so in a perl file, you need to load an existing session or load afresh.

    Code:
      use CGI::Session;
      my $cgi = new CGI;
      my $session = CGI::Session->load($cgi) or die CGI::Session->errstr;
    Then use the code as below, to get the data you need.

    Code:
    my $var = session->param('parameter');
    where parameter is the name given to the session value when it was stored.

    If you are wanting perl to load an existing session OR to cretae anew one the new need to use the line as shown below

    Code:
      use CGI::Session;
      my $cgi = new CGI;
      my $session = new CGI::Session($cgi) or die CGI::Session->errstr;
    for perl things: look up (in cpan)
    use DBI;
    use Session;
    use CGI;

    most of it is explained there in an easy-to-get-format. ERM; why did I say easy? I found it difficult until I got more help.

    bazz
    Last edited by bazz; 05-12-2009 at 01:38 AM.
    "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

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    92
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Should you use CGI::Session; or PHP::Session http://search.cpan.org/~miyagawa/PHP-Session-0.27/ ?

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    92
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Hey, I got it to work...

    first, I opened my php.ini file. It was previously saving the session files in /var/lib/php5. I changed it to save it in my "tmp" directory.

    Code:
    session.save_path = "/tmp"
    I also have:

    Code:
    ; Name of the session (used as cookie name).
    session.name = PHPSESSID
    I can use my login.php page to login like normal. It creates a session variable called "user" which stores the user id. Then, if I go to my perl page it will print out the user id.

    The perl code is:

    Code:
    #!/usr/bin/perl -w
    
    use CGI::Carp 'fatalsToBrowser';
    
    #try to get session username
    use PHP::Session;
    use CGI::Lite;
    my $session_name = 'PHPSESSID'; # change this if needed
    
    print "Content-type: text/plain\n\n";
    
    my $cgi = new CGI::Lite;
    
    my $cookies = $cgi->parse_cookies;
    print "cookies session_name: " . $cookies->{$session_name};
    if ($cookies->{$session_name}) {
     my $session = PHP::Session->new($cookies->{$session_name});
     # now, try to print uid variable from PHP session
     print "The user id is:" . $session->get('user');
    } else {
     print "can't find session cookie $session_name";
    }
    
    exit;
    Note: you have to have the following perl modules installed:

    CGI::Lite (http://search.cpan.org/~smylers/CGI-Lite-2.02/)
    and the PHP::SESSION one:
    http://search.cpan.org/~miyagawa/PHP-Session-0.27/

    You prolly know how to install them, but I didn't at first. Just untar them, and cd into the directory then run the following commands:

    Code:
    sudo perl Makefile.PL
    sudo make
    sudo make install
    Hope that works for you

  • #5
    New Coder
    Join Date
    Mar 2008
    Posts
    92
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Actually the above has a problem...I'm not sure how to fix it.

    Code:
    if ($cookies->{$session_name}) {
    checks to see if there is a cookie "PHPSESSID" to find the session name. If a user is logged in, and is logged out for some reason, and clicks the link/button to run the perl script, the cookie will still be there (you can't delete the php session cookie...only when the user closes their browser).

    So the following line gives an error:
    my $session = PHP::Session->new($cookies->{$session_name});

  • #6
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello - After setting the session variable in the Perl, how do I get it from the PHP? It seems to be setting just fine, but when I go to the PHP, it's not in my $_SESSION variables.

    Thanks!

  • #7
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    welcome to CF.

    Re-read this thread because it answers your question. if your session data is being stored by one language (php) and not retrieved by perl (or the other way around), make sure each session data is being stored in the same dir.

    see post #4

    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
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    perl script can't open the session file

    I'm using the approach suggested in this thread to access php session variables from a perl cgi script. I've got it working on my local Apache server, but on a client's server which is a VPS (Virtual Private Server) running Apache 2.2 the perl script can't open the session variable file that php saved in /var/lib/php5. The owner is www-data, which is the Apache user, and it has RW permission for owner only. The same situation exists on the local server, but it doesn't have a problem.

    Might this be because the VPS is imposing some kind of additional permission constraint? Could it be something to do with suEXEC? I'm not familiar with a VPS and don't know how it affects things.

    Does anyone have any advice? E.g. could I tell php to save it somewhere in the web tree, by creating a folder there owned by www-data?
    Regards,
    Peter

  • #9
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by spetsacdc View Post
    Actually the above has a problem...I'm not sure how to fix it.

    Code:
    if ($cookies->{$session_name}) {
    checks to see if there is a cookie "PHPSESSID" to find the session name. If a user is logged in, and is logged out for some reason, and clicks the link/button to run the perl script, the cookie will still be there (you can't delete the php session cookie...only when the user closes their browser).

    So the following line gives an error:
    my $session = PHP::Session->new($cookies->{$session_name});
    Same for me, did you figure it out yet? From what I gather:

    -perl gets a list of cookies from the client
    -parses it looking for a match
    -it does find a match, even though the session is expired the cookie is on the client
    -perl looks for the session for in php's session dir
    -the file doesnt exist as the session was destoryed/expired
    -apache prints this log because of that file missing
    [Wed Nov 04 13:23:43 2009] [error] [client 208.65.73.103] /tmp/sess_e471eaf0fe142c...78470: No such file or directory at /var/www/doobiest/perlses.pl line 13

    -perl script dies, where what I'd really like it to do is say the file wasnt found, thus invaliding the login process for the client.


    Any help would be appreciated.

  • #10
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK I figured it out.. that was quick :P

    The problem here is that, YES we are successfully finding the cookie on the client, and YES we are getting the hash for the session name. But NO we arent checking to see if the file exists before trying to open it, we just simply try to open it. Here's a fix, basically after getting the session hash, check if exists, if it does THEN execute the code to get the username etc. For me I dont use it, seeing a hash on the client that matches a hash on the server is good enough for me, as it's a single user, private site.

    Code:
    #!/usr/bin/perl -w
    
    use CGI::Carp 'fatalsToBrowser';
    
    #try to get session username
    use PHP::Session;
    use CGI::Lite;
    my $session_name = 'SESHUNNAME'; # change this if needed
    
    print "Content-type: text/plain\n\n";
    
    my $cgi = new CGI::Lite;
    my $cookies = $cgi->parse_cookies;
    my $file=$cookies->{$session_name};
    
    if (-e "/tmp/sess_$file") 
    {print "File exists!";}
    else 
    {print "File does not exist.";}
    
    #Put this stuff in the 'File Exists' portion.
    #if ($cookies->{$session_name}) { #this line isnt required any longer. we dont care if the cookie exists, we care if the cookie's hash matches a server session
    # my $session = PHP::Session->new($cookies->{$session_name});
    #  # now, try to print uid variable from PHP session
    #   print "The user id is:" . $session->get('user');
    #   } else {
    #    print "can't find session cookie $session_name";
    #    }
    
    #    exit;

  • #11
    New to the CF scene
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am having problems running this under php5. For some reason, it hangs up
    Code:
    my $session = PHP::Session->new($cookies->{$session_name});
    here.

    Indicating that session has not been found. I have tried installing the php.ini files to redirect the session. Has anyone ran into this problem or at least solved it?

  • #12
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Stumped

    I'm having a bit of an issue implementing this code.

    The scenario:
    I'm using Ryan's In/Out board to set up a statusboard (learning and function purposes), which is written in Perl. However, it doesn't have any form of user management/accountability right now and I am trying to embed/call this from within PHP. One of the more important things I am trying to do, is to pass the PHP session username information whenever someone makes a change to the statusboard. This requires passing the variable to Perl. I'm lucky that I've stumbled across this page since it has helped me out tremendously (gave me a great refresher to Perl and provided plenty of insight).

    However, whenever I run the script from the commandline, I get the following error:

    Content-type: text/plain

    [Fri Jan 6 10:17:09 2012] test.pl: Use of uninitialized value $file in concatenation (.) or string at /srv/www/cgi-bin/sboard/test.pl line 19.
    [Fri Jan 6 10:17:09 2012] test.pl: Use of uninitialized value $file in print at /srv/www/cgi-bin/sboard/test.pl line 32.
    File does not exist.0HASH(0xac1138)


    --------------------------------------------------------------------

    I understand that the code is not meant to be run from commandline, per se. But whenever I run it in the browser (called from PHP using echo exec), I also run into the same issue. Am I doing something wrong here? I'll admit I'm somewhat of a novice to programming/scripting, so there must be some stupid mistake I am running into. Below is the code that is being used:

    #!/usr/bin/perl -w
    use strict;
    use CGI::Carp 'fatalsToBrowser';

    #try to get session username
    use PHP::Session;
    use CGI::Lite;

    my $session_name = 'PHPSESSID'; # change this if needed

    print "Content-type: text/plain\n\n";

    my $cgi = new CGI::Lite;
    my $cookies = $cgi->parse_cookies;
    my $file=$cookies->{$session_name};
    #{$session_name};
    my $error=$cgi->is_error;

    if (-e "/tmp/sess_$file")
    {
    print "File exists!";
    print $file;
    print $error;
    print $cookies;
    print $session_name;
    my $session = PHP::Session->new($cookies->{$session_name});
    print "The user id is:" . $session->get('Username');
    }
    else
    {
    print "File does not exist.";
    print $file;
    print $error;
    print $cookies;

    }

    exit;


    I added a slew of print statements to help facilitate my understanding of what is going on (to hopefully fix the issue, but so far to no avail).

    Any and all help would be appreciated. Thanks in advance!

    Edit:
    Oh, yes, for the sake of clarity I should mention:

    OS: OpenSuSE 11.4 64 Bit, Repo vanilla desktop kernel (2.6.37.6-0.9-desktop)
    Perl: 5.12.3-11.18.1 from SuSE 11.4 update repo
    CPAN: perl-PHP-Session package, 0.27-3.1
    perl-CGI-Lite package, 2.02-3.1 (both from devel:languageserl:CPAN-C)
    PHP: 5.3.5-5.16.1 from SuSE 11.4 update repo
    Last edited by CommonOddity; 01-06-2012 at 03:36 PM. Reason: Added some info


  •  

    LinkBacks (?)

    1. 03-26-2014, 07:28 AM

    Posting Permissions

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