Gossamer Forum
Home : Products : Gossamer Links : Discussions :

getting user information in PHP?

Quote Reply
getting user information in PHP?
Hi,

it should not be too difficult to write but I have very low PHP knowledge so I was hoping that someone already has this and is also willing to share it.
We are using FPDF for some pdf Sheets. I would like to display some user information within the PDF if the user is logged in.
So I would have to check if there is a valid session from Links within my PHP script and get the user information.
The old PHP front end is probably no help because as far as I have seen it uses the sessionid of PHP.

Thank you

n || i || k || o

Last edited by:

el noe: Mar 30, 2011, 7:18 AM
Quote Reply
Re: [el noe] getting user information in PHP? In reply to
Are you using GLinks on its own, or with GCommunity?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] getting user information in PHP? In reply to
Hi Andy,

Thanks for your quick reply, just working with GLinks.

Regards

n || i || k || o
Quote Reply
Re: [el noe] getting user information in PHP? In reply to
Hi,

Have you checked the cookie vars when you are logged in? As far as I remember (without taking a closer look), you have a cookie which has a session ID... this is then links into the glinks_Sessions table, which you can work out if someone is still logged in from. Afraid my PHP is a bit crappy, so won't be much help on that front Wink

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] getting user information in PHP? In reply to
Hi Andy,

I was looking at Authenticate.pm :

Code:
sub auth_valid_session {
# -----------------------------------------------------------------------------
# This functions checks to see if the session is valid, and returns the
# username.
#
my $args = shift;
my $session_id = $IN->param('s') || $IN->cookie($CFG->{user_cookie_prefix} . 's') || return;
my $session;
unless ($session = GT::Session::SQL->new({
_debug => $CFG->{debug_level},
tb => $DB->table('Sessions'),
session_id => $session_id,
expires => $CFG->{user_session_length},
session_data => { sessions => $CFG->{user_sessions}, d => scalar($IN->param('d')) },
})) { # Possibly an expired session
GT::Session::SQL->new({
tb => $DB->table('Sessions'),
expires => $CFG->{user_session_length}
})->cleanup; # Clear out old sessions
return;
}

return $session->{info}->{session_user_id};
}

so the part with cookie or param seems not too difficult, getting the session from the table no problem BUT I am a little confused with the expires means valid session thing. I started to cut and paste something like that (indeed I only use cookies):

Code:
my $link = mysql_connect('localhost', 'user', 'pass');
if (!$link) {
die('not able to connect: ' . mysql_error());
}
my $session_id = $_COOKIE['s'];
my $session;
my $result = mysql_query("select * from glinks_Sessions where session_id like '$session_id'");
if (!$result) {
die "not able to connect: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 1) {
while ($row = mysql_fetch_assoc($result)) {
$USER = $row;
}
}
Quote Reply
Re: [el noe] getting user information in PHP? In reply to
Hi,

You don't want to use "LIKE", as that will be very slow.

Try:

Code:
my $result = mysql_query("select * from glinks_Sessions where session_id = '$session_id'");

The next part, is to grab the value from session_expires , which is the number of hours it should expire from. So you would want to do something like:

Code:
// get time from when they first logged in + x hours * 60 * 60 (so in seconds)
$expiry_time = $row['session_date'] + ($row['session_expire'] * 60 * 60);

Code:
if ($expiry_time < time()) {
// they are NOT logged in any more (session has expired)
} else {
// they are logged in
}

Apologies if there are syntax errors - its been many many years since I worked with PHP ;)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] getting user information in PHP? In reply to
Tanks very much once more Andy!

The result is neither nice nor neat but works for me and is maybe of use for somebody.
Code:
<?php
$link = mysql_connect('localhost', 'user', 'pass');
$db_selected = mysql_select_db('database', $link);
if (!$link) {
die('not able to connect: ' . mysql_error());
}
$session_id = $_COOKIE['s'] ? $_COOKIE['s'] : '';
if ($session_id) {
$result = mysql_query("select * from glinks_Sessions where session_id = '$session_id'");
if (mysql_num_rows($result) == 1) {
while ($row = mysql_fetch_assoc($result)) {
$session = $row;
$expiry_time = $row['session_date'] + ($row['session_expires'] * 60 * 60);
}
}
}
if ($session AND $expiry_time > time()) {
$result = mysql_query("select * from glinks_Users where Username = '$session[session_user_id]'");
if (mysql_num_rows($result) == 1) {
while ($row = mysql_fetch_assoc($result)) {
$USER = $row;
}
}
}

echo "$USER[Username] ";//test
?>

Regards

n || i || k || o

Last edited by:

el noe: Mar 30, 2011, 9:30 AM
Quote Reply
Re: [el noe] getting user information in PHP? In reply to
Cool, glad to have been of help Angelic

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!