Location: PHPKode > scripts > SPL and Iterators > spl-and-iterators/users/exemple.php
<?php
/**
 * exemple use of oUser package
 * @author Johan Barbier <hide@address.com>
 * @version 20061124
 */

/**
 * Defining some constants for the DB
 */
DEFINE ('SQL_LOGIN', 	'root');
DEFINE ('SQL_PWD', 	'pwd');
DEFINE ('SQL_DB', 	'bdd_test');
DEFINE ('SQL_HOST',     'localhost');

DEFINE ('SQL_USER_TABLE',     'users');

/**
 * Defining some variables
 */
$sMsg = 'Veuillez vous identifier';
$fXml = 'config.xml';
$sModForm = '';

/**
 * including required files (did not use __autoload because I'm lazy and have classes defined in the same files, other not...
 */
require_once 'dbfactory.cls.php';
require_once 'mysql.cls.php';
require_once 'class.oUser.php';

/**
 * getting the DB object
 */
try {
	$oDB = dbfactory::factory ('mysql', SQL_HOST, SQL_LOGIN, SQL_PWD, SQL_DB);
} catch (Exception $e) {
	echo $e -> getMessage (), '<br />';
	echo $e -> getLine (), '<br />';
	echo $e -> getFile (), '<br />';
}

/**
 * getting the oUser object
 */
try {
	$oUser = new oUser ($fXml, $oDB);
} catch (Exception $e) {
	echo $e -> getMessage (), '<br />';
	echo $e -> getLine (), '<br />';
	echo $e -> getFile (), '<br />';
}

/**
 * we already have a user (session is not empty)
 * so we get the user's properties
 * and display a new form to allow user to modify his name
 */
if (!empty ($_SESSION['USER']['ID'])) {
	try {
		$oUser -> getUser ($_SESSION['USER']['ID'], SQL_USER_TABLE);
	} catch (Exception $e) {
		echo $e -> getMessage (), '<br />';
		echo $e -> getLine (), '<br />';
		echo $e -> getFile (), '<br />';
	}
	$sMsg = 'Vous êtes actuellement '.$_SESSION['USER']['EMAIL'].'. Votre ID est : '.$_SESSION['USER']['ID'].'<br />Vous pouvez essayer une autre identification';
	$sModForm = <<<EOD
<form method="post" action="" id="fMod">
	<fieldset>
		<legend>Renseignez ou modifiez votre nom</legend>
		<p>
			<input type="text" name="sNom" />
		</p>
		<p>
			<input type="hidden" name="act" value="0" />
		</p>
		<p>
			<input type="button" value="Ok" style="cursor: pointer;" onclick="go ('fMod', 2);" />
		</p>
	</fieldset>
</form>
EOD;
}

/**
 * user chose to modify his name
 */
if (!empty ($_POST['act']) && $_POST['act'] === '2') {
	if (!empty ($_POST['sNom'])) {
		$bResult = $oUser -> modUser ($_SESSION['USER']['ID'], SQL_USER_TABLE, array ('NOM' => $_POST['sNom']));
		if (true === $bResult) {
			$sMsg = 'Modification effectuée';
			$oUser -> getUser ($_SESSION['USER']['ID'], SQL_USER_TABLE);
		} else {
			$sMsg = 'Modification échouée';
		}
	}

}

/**
 * user tries to authenticate
 * EMAIL and PASSWORD are shown in the XML config file as IDENT : it means they are used for authentication
 * EMAIl is shown in the XML config file as DEDOUBLE : it means that if we already have that EMAIl in the DB, and the other IDENT
 * fields is (are) wrong, there is a problem of authentication
 */
if (!empty ($_POST['act']) && $_POST['act'] === '1') {
	if (!empty ($_POST['sEmail']) && !empty ($_POST['sPwd'])) {
		try {
			/**
			 * checking authentication
			 */
			$bExists = $oUser -> checkIdent (SQL_USER_TABLE, array ('EMAIL' => $_POST['sEmail'], 'PASSWORD' => $_POST['sPwd']));
		} catch (Exception $e) {
			echo $e -> getMessage (), '<br />';
			echo $e -> getLine (), '<br />';
			echo $e -> getFile (), '<br />';
		}
		/**
		 * user exists and authentication is ok
		 * get user's properties
		 */
		if (true === $bExists) {
			try {
				$oUser -> getUser ($oUser -> oSession -> ID, SQL_USER_TABLE);
			} catch (Exception $e) {
				echo $e -> getMessage (), '<br />';
				echo $e -> getLine (), '<br />';
				echo $e -> getFile (), '<br />';
			}
			$sMsg = 'Vous êtes identifié '.$oUser -> EMAIL.'. Votre ID est : '.$oUser -> oSession -> ID.'<br />';
		/**
		 * user does not exist
		 * we create him
		 * and we get his properties
		 */
		} elseif (false === $bExists) {
			try {
				$oUser -> createUser (SQL_USER_TABLE, array ('EMAIL' => $_POST['sEmail'], 'PASSWORD' => $_POST['sPwd']));
				$oUser -> getUser ($oUser -> oSession -> ID, SQL_USER_TABLE);
			} catch (Exception $e) {
				echo $e -> getMessage (), '<br />';
				echo $e -> getLine (), '<br />';
				echo $e -> getFile (), '<br />';
			}
			$sMsg = 'Vous venez d\'être créé en base. Votre ID est : '.$oUser -> oSession -> ID;
		/**
		 * account exists in the DB (email here, because the xml config file shows EMAIL as DEDOUBLE, which means we have to check if the email already is in the DB)
		 * BUT the password is wrong (EMAIl and PASSWORD are shown in the config file as IDENT, which mean only them are used to check authentication)
		 * we inform the user of tha fact
		 */
		} else {
			$sMsg = 'Compte existant mais mauvaise identification. Veuillez réessayer.';
		}
	}
}

?>
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Exemple oUser</title>
	<script type="text/javascript">
		function go (sForm, iV) {
			with (document.getElementById(sForm)) {
				act.value = iV;
				submit ();
			}
		}
	</script>
	</head>
	<body>
		<form method="post" action="" id="fIdent">
			<fieldset>
				<legend>Identifiez vous</legend>
					<p>
						<input type="text" name="sEmail" value="<?php echo (!empty ($_SESSION['USER']['EMAIL']))?$_SESSION['USER']['EMAIL']:''; ?>" />
					</p>
					<p>
						<input type="password" name="sPwd" />
					</p>
					<p style="display: none;">
						<input type="hidden" name="act" value="0" />
					</p>
					<p>
						<input type="button" value="Ok" style="cursor: pointer;" onclick="go ('fIdent', 1);" />
					</p>
			</fieldset>
		</form>
		<br /><br />
		<?php echo $sModForm; ?>
		<p>
			<?php echo $sMsg; ?>
		</p>
		<p>
			<strong>RECAP</strong><br /><br />
			<?php
				$oUser -> FILTER = 'VALUE';
				$aProps = $oUser -> getProps ();
				foreach ($aProps as $sV) {
					echo $aProps -> getInnerIterator () -> getSubIterator (0) -> key (), ' : ', $sV, '<br />';
				}
			?>
		</p>
	</body>
</html>
Return current item: SPL and Iterators