and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Is it possible for Perl to read session cookies set/written by a PHP script? I would like to use AWSTATS (written in Perl) and called from my custom-built CMS (written in PHP), and I currently use htaccess to password protect the AWSTATS folder but am trying to move away from it.
In CMS, a session is set if your login details provided are correct as follows:
use CGI;
my $cgi = new CGI;
my $name = $cgi->cookie("username");
if ($name) { # if cookie is set
# redirect to authorised page
}
else {
# redirect to unauthorised page
}
But the cookie is always empty even when the PHP shows that it is not. Obviously I have limited experience with Perl so any advice would be greatly appreciated.
Btw, I already tried the following without success:
- PHP::Interpreter -- need to have --embed flag but I can't re- configure the PHP and would rather not mess around with PHP settings!
- PHP::Include -- Gave "Undefined subroutine include_php_vars" error when I ran the PL file. Google produces nothing.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
$_SESSION['username'] is not a cookie, it is session variable, sessions are saved on the server in serialized format and the session id is saved in a cookie in the client browser.
I think you can try to read the session container from perl based on the session id cookie and retrieve the username.
Many thanks for your responses! Sorry miro_igov - I keep forgotting!
VladSun / timvw -- maybe I am wrong but are these simply not PHPSESSID sessions (with a value of something like e2xj5ep5ql65k108u41ktamc30) which I don't require -- I meant the actual value of a particular session variable. So when you login and a variable is set ($_SESSION['username']), the value is "admin".
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="timvw"]Please have another look at the module, because it does give you read/write access to the values themselves..[/quote]
This is what I have:
[syntax="perl"]
use PHP::Session;
use CGI::Lite;
my $session_name = 'username'; # change this if needed
print "Content-type: text/plain\n\n";
my $cgi = new CGI::Lite;
my $cookies = $cgi->parse_cookies;
if ($cookies->{$session_name}) {
my $session = PHP::Session->new($cookies->{$session_name});
# now, try to print uid variable from PHP session
print "uid:",Dumper($session->get('uid'));
} else {
print "can't find session cookie $session_name";
}
After logging in via CMS and confirming that $_SESSION['username'] is set/saved, the Perl code above just did not find it ("can't find session cookie" message). :/ AM I missing something silly?
Btw, the sess_* are not in /tmp and instead are in /var/lib/php/session/sess_* -- should I set the path in the constructor new?
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Afaik, it's documentation clearly says that you should pass in the session.save_path in the constructor (if it's not in the default /tmp)... As you (may or may not know) the session identifier is by default PHPSESSID (and usually there is no reason to change this)... So i wonder why you set it to 'username'...
Anyway, here is the code that i used:
[syntax="perl"]
use lib '/opt/users/timvw/perl/'; # i added this line because i haven't installed the module in my main repository...
use PHP::Session;
use CGI::Lite;
my $session_name = 'PHPSESSID';
print "Content-type: text/plain\n\n";
my $cgi = new CGI::Lite;
my $cookies = $cgi->parse_cookies;
if ($cookies->{$session_name})
{
my $session = PHP::Session->new($cookies->{$session_name}, { save_path => '/var/php/' } );
# now, try to print php's $_SESSION['username']
print $session->get('username');
}
else
{
print "can't find session cookie $session_name";
}
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]