Create User Session (PHP 5)

PHP 5 Sample Code

This is a sample PHP code that:
  1. Authenticates.
  2. Uses web services to create a normal login session for a specified user.
The script uses an INI file to determine the authentication information.  Note the attached library files used by the script.



<?php
// set home path
$homePath = '';

// only show errors
error_reporting(E_ERROR | E_PARSE);

// login and obtain web service handle $lmsBinding
// NOTE: authentication information saved in referenced INI file
require_once($homePath . 'includes/init_session.inc');


//********************************************************************
// constants
//********************************************************************

// CHANGE ME: these are session parameters that can be changed
define("QUIZ_RETURN_URL", "http://www.firmwater.com");
define("QUIZ_TIMEOUT_URL", "http://www.firmwater.com/?to=1");
define("QUIZ_TIMEOUT_MINUTES", 30);


//********************************************************************
// globals
//********************************************************************

// CHANGE ME: this is the person that the user session will be created for
$licensee = 'yourcompany';
$username = 'asmith';
$firstName = 'Allison';
$lastName = 'Smith';


//********************************************************************
// functions
//********************************************************************

function createPersonObject($licensee, $username, $firstName, $lastName) {

	$person = new LmsPersonObject();
	$person->LicenseeId = $licensee;
	$person->Username = $username;
	$person->FirstName = $firstName;
	$person->LastName = $lastName;
	$person->AdministrativePrivilege = 'unknown';

	return $person;
}

function createUserSession($binding, $person) {

	$params = new UserSessionParams('normalLogin');
	$params->ReturnUrl = QUIZ_RETURN_URL;
	$params->TimeoutUrl = QUIZ_TIMEOUT_URL;
	$params->TimeoutMinutes = QUIZ_TIMEOUT_MINUTES;
	$params->CloseWindowOnExit = false;
	
	$url = null;
	try { $url = $binding->CreateUserSessionWithParams($person, $params)->Url; }
	catch (LmsService1_Exception $e) {
		print 'Exception caught by ' . __FILE__ . ' on line ' . __LINE__ . ': ';
		print $e->getMessage();
		print_r($binding->getLastRequest());
		print_r($binding->getLastResponse());
		exit;
	}
	
	return $url;
}


//********************************************************************
// main
//********************************************************************

// process form variables if submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	// create user session
	$person = createPersonObject($licensee, $username, $firstName, $lastName);
	$url = createUserSession($lmsBinding, $person);
	if ($url != null) {
		header('Location: ' . $url);
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Creating a UserSession...</title>
</head>

<body>
<p>Click the button to login.</p>
<form method="post">
	<input type="submit" value="Go!" />	
</form>
</body>
</html>
ċ
LmsService1.inc
(15k)
Stefan Leyhane,
Aug 11, 2014, 12:10 PM
ċ
init_session.inc
(2k)
Stefan Leyhane,
Oct 28, 2011, 8:33 AM
ċ
lms.ini
(0k)
Stefan Leyhane,
Sep 19, 2011, 1:56 PM
Comments