Stuck with session management

xravenx

Joined: 2007-10-22
Posts: 3
Posted: Mon, 2007-10-22 09:17

I'm trying to integrate Gallery 2 with my PHP/mySQL driven site.

Right now, my emApp uses a users table where there's a user_num (unique to each user), user_id (login name). These variables are passed into the session array once a user has logged in to my website.
I want the user to be able to login to my site, and then not have to login to gallery again. I've got the login part working. Problem comes with sessions and logging out.

I have a gallery.php file which has the following code:


<?php
session_name('MySessionName'); // Choose session name
session_start(); // Initialize a session

require_once('./includes/g2embed.inc.php'); 
?>

My g2embed.inc.php file has a create user on-the-fly as seen here:

<?php
$data = runGallery();
$data['title'] = (isset($data['title']) && !empty($data['title'])) ? $data['title'] : 'Gallery';

if (isset($data['bodyHtml'])) {
print <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>{$data['title']}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
{$data['javascript']}
{$data['css']}
</head>

<body>
{$data['bodyHtml']}
</body>
</html>
EOF;
}


function runGallery() {

    require_once('./gallery2/embed.php');
    
	$data = array();
	
	$emAppUserId = $_SESSION['user_num'];
	$embedUri = '/gallery.php';
	$g2Uri = '/gallery2/';
	$loginRedirect = '/index.php';

	if (isset($_SESSION['user_id'])) {
		// if user is logged in, set user ID to emApp's session user_id
		$uid = $_SESSION['user_id'];
	} else {
		// if anonymous user, set g2 activeUser to ''
		$uid = '';
	}
	
	$ret = GalleryEmbed::init(array('embedUri' => $embedUri, 'g2Uri' => $g2Uri, 'fullInit' => true, 'loginRedirect' => $loginRedirect, 'activeUserId' => $emAppUserId));
	
	// Display login link
	 GalleryCapabilities::set('login', true);
	
	// user interface: you could disable sidebar in G2 and get it as separate HTML to put it into a block
    // GalleryCapabilities::set('showSidebarBlocks', false);
	
	if ($ret) {
		 /* Error! */
		 /* Did we get an error because the user doesn't exist in g2 yet? */
		 $ret2 = GalleryEmbed::isExternalIdMapped($emAppUserId, 'GalleryUser');
		 if ($ret2 && $ret2->getErrorCode() & ERROR_MISSING_OBJECT) {
			 /* The user does not exist in G2 yet. Create in now on-the-fly */
			 $ret = GalleryEmbed::createUser($emAppUserId, array('username' => $uid));

			 if ($ret) {
				 /* An error during user creation. Not good, print an error or do whatever is appropriate 
				  * in your emApp when an error occurs */
				 print "An error occurred during the on-the-fly user creation <br>";
				 print $ret->getAsHtml();
				 exit;
			 }
		 } else {
			 /* The error we got wasn't due to a missing user, it was a real error */
			 if ($ret2) {
				 print "An error occurred while checking if a user already exists<br>";
				 print $ret2->getAsHtml();
			 }
			 print "An error occurred while trying to initialize G2<br>";
			 print $ret->getAsHtml();
			 exit;
		 }
	 }

	/* At this point we know that either the user either existed already before or that it was just created
	 * proceed with the normal request to G2 */
	$g2moddata = GalleryEmbed::handleRequest();
	
    // show error message if isDone is not defined
    if (!isset($g2moddata['isDone'])) {
      $data['bodyHtml'] = 'isDone is not defined, something very bad must have happened.';
      return $data;
    }	

    // exit if it was an immediate view / request (G2 already outputted some data)
    if ($g2moddata['isDone']) {
	exit; 
    }
   
    // put the body html from G2 into the xaraya template 
    $data['bodyHtml'] = isset($g2moddata['bodyHtml']) ? $g2moddata['bodyHtml'] : '';
	
    // get the page title, javascript and css links from the <head> html from G2
    $title = ''; $javascript = array();	$css = array();
 
    if (isset($g2moddata['headHtml'])) {
      list($data['title'], $css, $javascript) = GalleryEmbed::parseHead($g2moddata['headHtml']);
	  $data['headHtml'] = $g2moddata['headHtml'];
    }
    
    
    /* Add G2 javascript  */
    $data['javascript'] = '';
    if (!empty($javascript)) {
      foreach ($javascript as $script) {
	     $data['javascript'] .= "\n".$script;
      }
    }

    /* Add G2 css  */
    $data['css'] = '';
    if (!empty($css)) {
      foreach ($css as $style) {
	     $data['css'] .= "\n".$style;
      }
    }

    // sidebar block
    if (isset($g2moddata['sidebarBlocksHtml']) && !empty($g2moddata['sidebarBlocksHtml'])) {
      $data['sidebarHtml'] = $g2moddata['sidebarBlocksHtml'];
    }
    
    return $data;
} 


?>

My logout.php script has this:

<?php
// Log out of Gallery
require_once('./gallery2/embed.php');
$ret = GalleryEmbed::logout();
?>

My problem is this: using phpMyAdmin, I've been monitoring the g2_SessionMap table.
When a user that's already logged into my site goes to gallery.php, it creates the user and appropriate user mapping in the g2_ExternalIdMap table appropriately if the user doesn't exist. If the user exists, it logs the user in as expected. But, if the user continues to navigate any page within the gallery, it generates another session in the g2_SessionMap table. If the user tries to logout using logout.php, their sessions in g2_SessionMap doesn't get deleted. How do I make it so that a user that's logged in will continue to use the same gallery session to navigate the gallery? How do I get the logout to delete the appropriate sessions from teh g2_SessionMap table?

As a note, the documentation provided for writing your own integration is very complete but very very difficult to follow if you are not an expert at PHP. I had to spend several hours to get to where I am right now.

Anyhow, any help would be appreciated. I really really love Gallery and would be very happy if I can integrate it fully with my website.

Thanks in advance!

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2007-10-26 14:44

@logout: don't ignore $ret. you should check it. e.g. with
if ($ret) {
//log the error, (error can be fetched with $ret->getAsHtml() )
// while developing, just print the error:
print $ret->getAsHtml();
};

@sessions:

> But, if the user continues to navigate any page within the gallery, it generates another session in the g2_SessionMap table.

did the user successfully receive a cookie when logged in for the first time? maybe output starts before g2 can set the cookie header.

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

 
xravenx

Joined: 2007-10-22
Posts: 3
Posted: Thu, 2007-11-01 02:12

Thanks for the tips valiant.

I got it working. It seems I can't use require_once('./includes/g2embed.inc.php');
It breaks it.

One file must contain all the code for embed display of the gallery.

Thanks so much again!