Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Run PHP applications in Apache Geronimo

The PHP Java Bridge provides full support

Tyler Anderson (tyleranderson5@yahoo.com), Freelance Writer, Stexar Corp.
Tyler Anderson formerly worked for DPMG.com, an SEO company, for whom he wrote proprietary SEO software. He graduated with a degree in computer science from Brigham Young University in 2004 and has just graduated with a Master of Science degree in Computer Engineering in December 2005, also from Brigham Young University. He is currently an engineer for Stexar Corp., based in Beaverton, Oregon. You can reach Tyler at tyleranderson5@yahoo.com.

Summary:  PHP has been a popular scripting language for some time. However, with the growing buzz over Java™ technology and Apache Geronimo, a J2EE-certified application server, many experienced developers shy away from using PHP with Geronimo, because only JavaServer Pages (JSP) is supported out of the box. The PHP Java Bridge solves this problem by providing full support for PHP on Geronimo and for sharing sessions across both PHP and JSP scripts.

Date:  07 Feb 2006
Level:  Intermediate PDF:  A4 and Letter (569 KB | 25 pages)Get Adobe® Reader®

Activity:  10633 views
Comments:  

Administer the IQ test

Everything's in line for you to take the IQ test. Now you initialize the session for the IQ test and iterate through five questions to determine a visiting user's fictitious IQ score. Each answer is saved as a session variable and retrieved in the next section for computing the final IQ score.

Processing "take the test" requests

When a user clicks the link shown in Figure 4 to begin the IQ test, you need to be able to capture that with your script and modify what your PHP displays back to that user accordingly. The code shown in Listing 7 does just that.

The first line checks to make sure that the link has been clicked by ensuring that the test variable in the URL or GET array is true. If the test hasn't begun yet, then the session variables get initialized with a call to the initTest() function -- which you define next in Initialize the session variables -- and a session variable, indicating that the test has begun, gets set to true. Also, if a visitor ever returns to the main start page where the Take the IQ test here text gets displayed, all the session variables get destroyed with the call to $session->destroy().






Listing 7. Starting the test
<?php
...
$session = java_session();
include('includes/functions.php');

if($_GET['test'] != '' && $_GET['test'] === "true") {
    if($session->get(TEST_BEGUN) == null) {
        include('header.php');
        $session->put(TEST_BEGUN, new java("java.lang.Boolean", true));
        initTest($session);
    }
    else{
...
    }
}
else{ 
    $session->destroy();
    include('header.php');
    print "Take your IQ test <a href='index.php?test=true'>".
          "here</a><br>";
}
include('footer.php');
?>


Initialize the session variables

Here you initialize the session variables and display the first question back to the user. This enables your code to track which questions visitors have answered and which ones they haven't answered. Define the initTest() function in the functions.php file, as shown in Listing 8.


Listing 8. Initializing the session variables
function initTest($session){
    $session->put(QUESTION_ONE, new java("java.lang.Boolean", false));
    $session->put(QUESTION_TWO, new java("java.lang.Boolean", false));
    $session->put(QUESTION_THREE, new java("java.lang.Boolean",
                                           false));
    $session->put(QUESTION_FOUR, new java("java.lang.Boolean", false));
    $session->put(QUESTION_FIVE, new java("java.lang.Boolean", false));
    questionOne($session);
}
 

The session variable for each question, which indicates whether the question has been answered or not, is set to false. When a question gets answered, you set the session variable to true (later in the tutorial).

Now you write the code to check whether or not a visitor has answered the first question, and if not, display it again. Continue defining the index.php file, as shown in Listing 9.


Listing 9. Checking to see if the first question has been answered
...
        $session->put(TEST_BEGUN, new java("java.lang.Boolean", true));
        initTest($session);
    }
    else{
        do {
            if($session->get(QUESTION_ONE)->booleanValue() === false) {
                include('header.php');
                questionOne($session);
                break;
            }
        } while(false);
    }
}
else{
    include('header.php');
    $session->destroy();
...

If the first question wasn't immediately answered, then it displays again until it is answered. Next you see the form and question used for the first question in the IQ test.


Display question one

Here you use an HTML form to obtain feedback from the user on whether or not they know the answer to the given question. Define the questionOne() function, as shown in Listing 10.


Listing 10. Displaying the first question
function questionOne($session){
    print "<h3>Question One</h3>";
    print "<form action='index.php?test=true' method='POST'>".
         "Are trees biotic or abiotic? ".
         "<input name='".QUESTION_ONE_ANSWER."' /><br />".
         "<input type='submit' value='Submit' /></form>";
}

Note that the form keeps passing along the test=true variable in the URL. Transmission is via POST; answers to form questions don't belong in the URL, because they can often be sensitive information. On top of that, variables passed via GET are saved in the URL when a page is bookmarked, and you wouldn't want someone to return to this page and unexpectedly answer a question they didn't intend to. See the browser output for question one in Figure 5.


Figure 5. Displaying question one
Displaying question one

Next you process the answer submitted via this form.


Process the answer to question one

When a visitor submits an answer, you need to look for it in the POST array. Continue defining the index.php file, as shown in Listing 11.


Listing 11. Processing the answer to question one
...
include('includes/functions.php');

if($_POST[QUESTION_ONE_ANSWER] != ''){
    $ans = $_POST[QUESTION_ONE_ANSWER];

    $session->put(QUESTION_ONE, new java("java.lang.Boolean", true));
    $session->put(QUESTION_ONE_ANSWER,
                  new java("java.lang.String", $ans));
}

if($_GET['test'] != '' && $_GET['test'] === "true") {
    if($session->get(TEST_BEGUN) == null) {
...

Listing 11 checks to see if the POST array contains the answer to question one. If it does, it saves the answer as a session variable and modifies the session variable indicating whether or not question one has been answered from false to true. The next few panels go over displaying and processing the rest of the IQ test questions.


Display question two

Similar to question one, if question one is answered and question two is unanswered, then question two needs to be displayed. Do so by continuing to define the index.php file, as shown in Listing 12.


Listing 12. Displaying question two
...
        do {
            if($session->get(QUESTION_ONE)->booleanValue() === false) {
                include('header.php');
                questionOne($session);
                break;
            } else
            if($session->get(QUESTION_TWO)->booleanValue() === false) {
                include('header.php');
                questionTwo($session);
                break;
            }
        } while(false);
...

The code in the if statement in bold executes when question one has been answered and question two has not. Also, it displays question two back to the visiting user by calling the questionTwo() function. Again, this is similar to question one. Define the questionTwo() function, as shown in Listing 13.


Listing 13. Displaying question two's form
function questionTwo($session){
    print "<h3>Question two</h3>";
    print "<form action='index.php?test=true' method='POST'>".
         "Are you biotic or abiotic? ".
         "<input name='".QUESTION_TWO_ANSWER."' /><br />".
         "<input type='submit' value='Submit' /></form>";
}

Very similar to question one with a different question, the form is shown to the user, and submitting an answer brings you to the next panel.


Process answers

This section processes the answers submitted from the forms for questions two through five. Continue defining the index.php file, as shown in Listing 14.


Listing 14. Processing answers for questions two through five
...
    $session->put(QUESTION_ONE, new java("java.lang.Boolean", true));
    $session->put(QUESTION_ONE_ANSWER,
                  new java("java.lang.String", $ans));
}
else if($_POST[QUESTION_TWO_ANSWER] != ''){
    $ans = $_POST[QUESTION_TWO_ANSWER];
    
    $session->put(QUESTION_TWO, new java("java.lang.Boolean", true));
    $session->put(QUESTION_TWO_ANSWER,
                  new java("java.lang.String", $ans));
}
else if($_POST[QUESTION_THREE_ANSWER] != ''){
    $ans = $_POST[QUESTION_THREE_ANSWER];
    
    $session->put(QUESTION_THREE, new java("java.lang.Boolean", true));
    $session->put(QUESTION_THREE_ANSWER,
                  new java("java.lang.String", $ans));
}
else if($_POST[QUESTION_FOUR_ANSWER] != ''){
    $ans = $_POST[QUESTION_FOUR_ANSWER];
    
    $session->put(QUESTION_FOUR, new java("java.lang.Boolean", true));
    $session->put(QUESTION_FOUR_ANSWER,
                  new java("java.lang.String", $ans));
}
else if($_POST[QUESTION_FIVE_ANSWER] != ''){
    $ans = $_POST[QUESTION_FIVE_ANSWER];
    
    $session->put(QUESTION_FIVE, new java("java.lang.Boolean", true));
    $session->put(QUESTION_FIVE_ANSWER,
                  new java("java.lang.String", $ans));
}

if($_GET['test'] != '' && $_GET['test'] === "true") {
    if($session->get(TEST_BEGUN) == null) {

As each question gets answered, the session variable indicating whether it has been answered or not gets set from false to true, and the answer to the question is stored as a separate session variable.


Display the other questions

Similar to questions one and two, unanswered questions need to be answered in succession. If the session variable indicates that the question remains unanswered, then the user is prompted to answer the given IQ test question. Continue to define the index.php file, as shown in Listing 15.


Listing 15. Displaying the rest of the unanswered questions
...
               questionTwo($session);
               break;
           } else
           if($session->get(QUESTION_THREE)->booleanValue() === false){
               include('header.php');
               questionThree($session);
               break;
           } else
           if($session->get(QUESTION_FOUR)->booleanValue() === false){
               include('header.php');
               questionFour($session);
               break;
           } else
           if($session->get(QUESTION_FIVE)->booleanValue() === false){
               include('header.php');
               questionFive($session);
               break;
           } 
...
       } while(false);
...

With code in place to display the questions, compete the functions.php file by defining the following three functions: questionThree(), questionFour(), and questionFive(), as shown in Listing 16.


Listing 16. Displaying questions three through five
function questionThree($session){
    print "<h3>Question three</h3>";
    print "<form action='index.php?test=true' method='POST'>".
         "How many sides are in a square? ".
         "<input name='".QUESTION_THREE_ANSWER."' /><br />".
         "<input type='submit' value='Submit' /></form>";
}

function questionFour($session){
    print "<h3>Question four</h3>";
    print "<form action='index.php?test=true' method='POST'>".
         "How many sides are in a circle? ".
         "<input name='".QUESTION_FOUR_ANSWER."' /><br />".
         "<input type='submit' value='Submit' /></form>";
}

function questionFive($session){
    print "<h3>Question Five</h3>";
    print "<form action='index.php?test=true' method='POST'>".
         "What is 1 plus 1? ".
         "<input name='".QUESTION_FIVE_ANSWER."' /><br />".
         "<input type='submit' value='Submit' /></form>";
}

Each question is now displayed appropriately. Now, on to shared PHP/JSP sessions.

6 of 11 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Java technology
ArticleID=102950
TutorialTitle=Run PHP applications in Apache Geronimo
publish-date=02072006
author1-email=tyleranderson5@yahoo.com
author1-email-cc=troy@backstopmedia.com