Completing a Configuration for Chrome and a Server - Logic changes
(Page 4 of 5 )
Next, we change our doCommandXUL.php frontend script to execute the logic that decides what interface to serve. The only change from the last iteration of the code is to alter the path that actually checks the result if the user entered data into the name and password fields.
The code takes advantage of the PHPexplodefunction to break up the returned string into an array of name-value pairs.
We use the PHPsubst(inString,startingChar,lastChar)functions to break the result array’s strings into values, given the fact that we know the length of the names being used as tags. If the first returned value istrue, we save a string that holds session information into a variable, and include the PHP file that holds the XUL interface content. If the first returned value isfalse, we build a message that flags an unregistered user:
else {
$retString = check_user($uName,$uPass);
$resArray = explode(',',$retString);
if ($resArray[0] == 'retcode=true') {
$lastLoginTime = 'Last login was '.substr($resArray[1],
11,strlen($resArray[1])); // extract last session
require('startupScreen.php');
}
else { // invalid user, send rejection page
echo '<h1>Sorry!</h1>';
echo '<h2>You are not registered to use this service.</h2>';
} // invalid user
}
?>
We need to insert another subtle change into the newssearch.js file. Because the login button is no longer on our startup screen (the doCommandXUL.php file is generating it), we can no longer try to attach a login script upon initialization. We could edit the code to remove all initialization references, or (if we wanted to use the same file for both local and served versions) we could add a test to make certain the button exists before attaching an event listener:
function initialize(){
try {
document.getElementById("B1").
addEventListener("command",genericBtnHandler,true);
document.getElementById("B2").
addEventListener("command",genericBtnHandler,true);
document.getElementById("B3").
addEventListener("command",genericBtnHandler,true);
//
// Add a login script
if (document.getElementById("loginButton")) {
document.getElementById("loginButton").
addEventListener("command",doLogin,true);
}
}
catch (e) {
alert ("Exception: " + e);
}
}
Finally, we change the segment of the XUL source in our startupScreen.php file that manages the status label to include the PHP directives to substitute the$lastLoginTimevariable into thelabelattribute of the status display:
<hbox >
<!-- stack info and resize horizontally -->
<!-- right align our status bar -->
<statusbar id="status-bar" class="chromeclass-status">
<statusbarpanel id="status-text"
<?php
echo('label=\''."$lastLoginTime".'\'/>');
?>
</statusbar>
<spacer flex="1"/>
</hbox>
Now referencing the doCommandXUL.php file from the Firefox URL will present the same interaction with the user, except with the interface delivered from the web server. Figure 4-11 shows our XUL-served page for a successful login and a failed login.
Next: Summary >>
More Web Standards Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of Programming Firefox, written by Kenneth C. Feldt (O'Reilly, 2007; ISBN: 0596102437). Check it out today at your favorite bookstore. Buy this book now.
|
|