Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 05-12-2009, 01:21 AM   PM User | #1
madpar3
New to the CF scene

 
Join Date: Mar 2009
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
madpar3 is an unknown quantity at this point
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.
madpar3 is offline   Reply With Quote
Old 05-12-2009, 02:36 AM   PM User | #2
bazz
Senior Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 4,330
Thanks: 35
Thanked 151 Times in 147 Posts
bazz will become famous soon enoughbazz will become famous soon enough
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
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.
Useful MySQL resource
Useful MySQL link

Last edited by bazz; 05-12-2009 at 02:38 AM..
bazz is offline   Reply With Quote
Old 05-22-2009, 07:59 PM   PM User | #3
spetsacdc
New Coder

 
Join Date: Mar 2008
Posts: 92
Thanks: 19
Thanked 0 Times in 0 Posts
spetsacdc is an unknown quantity at this point
Should you use CGI::Session; or PHP::Session http://search.cpan.org/~miyagawa/PHP-Session-0.27/ ?
spetsacdc is offline   Reply With Quote
Old 05-22-2009, 08:42 PM   PM User | #4
spetsacdc
New Coder

 
Join Date: Mar 2008
Posts: 92
Thanks: 19
Thanked 0 Times in 0 Posts
spetsacdc is an unknown quantity at this point
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
spetsacdc is offline   Reply With Quote
Old 05-23-2009, 07:02 AM   PM User | #5
spetsacdc
New Coder

 
Join Date: Mar 2008
Posts: 92
Thanks: 19
Thanked 0 Times in 0 Posts
spetsacdc is an unknown quantity at this point
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});
spetsacdc is offline   Reply With Quote
Old 07-16-2009, 08:32 PM   PM User | #6
dizee
New to the CF scene

 
Join Date: Jul 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
dizee is an unknown quantity at this point
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!
dizee is offline   Reply With Quote
Old 07-16-2009, 10:26 PM   PM User | #7
bazz
Senior Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 4,330
Thanks: 35
Thanked 151 Times in 147 Posts
bazz will become famous soon enoughbazz will become famous soon enough
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.
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Old 10-13-2009, 05:43 PM   PM User | #8
mlcprs
New to the CF scene

 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mlcprs is an unknown quantity at this point
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
mlcprs is offline   Reply With Quote
Old 11-04-2009, 07:54 PM   PM User | #9
doobiest
New to the CF scene

 
Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
doobiest is an unknown quantity at this point
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.
doobiest is offline   Reply With Quote
Old 11-04-2009, 08:13 PM   PM User | #10
doobiest
New to the CF scene

 
Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
doobiest is an unknown quantity at this point
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;
doobiest is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:15 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.