How do you access Gallery's 'user ID' from PHP rather than from Smarty?

chazzer

Joined: 2007-05-24
Posts: 2
Posted: Thu, 2007-05-24 14:24

I am trying to add a few PHP-driven pages onto the side of my Gallery2 application to make a website. The extra PHP pages will be for users to write and save a variety of personal information under their Gallery2 username and password (i.e. they would sign-in on the website, where they would be able to edit their gallery as well as edit their information under the same session). I don't want to use any blog/3rd party application to do this as what I need to do is not covered elsewhere. I thought this would be a simple task, as I thought all I would need was to identify the 'user id' of the user once they sign in - I could then use this 'user id' in some new PHP tables, that I will create, against their personal information to only allow a user's information to be edited by themself once they had logged in). I'm happy writing the PHP stuff once I've been able to access the 'user id' variable in PHP (i.e. not in Smarty).

To test this - once I can 'echo' the 'user ID' in a PHP file I'm happy doing the rest, as it will mean I've managed to identify and use the variable. The principle is that I want to be able to do the same as this Smarty code, but in PHP:
{if $user.isRegisteredUser}
Welcome {$user.userName}!
{/if}

I thought the other way might be like this (http://gallery.menalto.com/node/58611) but I can't get that to display either :
<?php echo $gallery->user->username; ?>

I tried these 2 methods, neither of which I can get to work:

1) embedding Gallery2 in my website and accessing it via an external PHP page within which I might be able to use the 'user ID' variable:
- followed supersidr's excellent instructions under: http://gallery.menalto.com/node/64119
- I then successfully get access to my Gallery2 pages from an external page (galleryindex.php)
- but I still can't use PHP in the galleryindex.php file to display a username, however hard I try

2) adding a new template.tpl file that uses an 'include' to access another PHP file, within which I use the 'user ID' variable:
I cannot get a PHP include file to work inside a Smarty template - I've tried each of these:
- inside the Smarty code: {include file='filename.php'}
- inside the Smarty code: {include_php file="/path/to/filename.php"}
- inside the Smarty code: {php} include filename.php; {/php}
- inside the Smarty code: {php} include 'filename.php'; {/php}

I'm really struggling to get this to work (having spent 2 days searching the forums and everywhere else since). Can anyone help, as I'm sure I'm missing something simple that might help others trying to embed their gallery into their website?

Thanks in advance.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2007-05-25 11:18

there are various approaches you could use here:
- develop your own, small g2 module
- do the template include that you suggested
- use GalleryEmbed at the start of your external php script(s) to get the active g2 user
- ...

what do you prefer?
if your scripts are independent of your gallery, you probably want to use the GalleryEmbed approach.

i assume here that your script file is called "test.php" and that it's located in your gallery2 folder.
if it's not located in your gallery2 folder, you need to change the cookie path in "site admin -> general" to be a parent folder of your g2 folder and the other script file.

in your test.php script, do the following:

<?php
require_once(dirname(__FILE__) . '/embed.php');
$ret = GalleryEmbed::init(array('fullInit' => true));
if ($ret) die($ret->getAsHtml());
global $gallery;
$userId = $gallery->getActiveUserId();

// Now you may want to find out whether it is a registered user...
list ($ret, $anonymousUserId) = GalleryCoreApi::getAnonymousUserId();
if ($ret) die($ret->getAsHtml());
$isRegisteredUser = !($userId == $anonymousUserId);

...

?>

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage

 
chazzer

Joined: 2007-05-24
Posts: 2
Posted: Wed, 2007-06-06 08:02

That works a treat - thank you !

 
paulrod

Joined: 2007-09-21
Posts: 1
Posted: Mon, 2007-09-24 17:31

I am trying to accomplish somewhat the same (have users checked for g2 login from outside php pages) and came across this post.

I am new to php and am confused about the code you have in this reply.
What is happening in this?

list ($ret, $anonymousUserId) = GalleryCoreApi::getAnonymousUserId();
if ($ret) die($ret->getAsHtml());
$isRegisteredUser = !($userId == $anonymousUserId);

If I wanted an If Then around this how can I accomplish that. For example;
If (user is not logged in) {
then redirect
}
present the secure information?

In a nutshell; I need a way to authenticate users using the G2 db and session/cookie information.

I have set in Site Admin "/" as the location for cookies.

I appreciate your help.

Paul