Chapter 1 Exercises

Exercise 3: Testing Your Script

Unlike HTML, which can be tested on your own computer using a web browser, your PHP scripts need to be run from a PHP-enabled server to see what the output will look like.

If you prefer to test and view your PHP scripts on your local server, you will need to save or move them to the web document root of XAMPP. This is located in your XAMPP folder.

  • For Windows:  c:\XAMPP\htdocs
  • For Mac: /Applications/XAMPP/htdocs

For the most part, and for your graded assignments you will need to transfer them to their destination using FTP (File Transfer Protocol), test and work with the files at the remote server, and submit the URLs for grading in Moodle.

Note 1: It is not enough that a script or HTML document works on your local server. It only has value if it works well on a production server, which is a real web server. As you progress through this course remember to organize your files into their separate directories by chapter number, both on your local server, and the production server. Always make sure to have tested your scripts on the production server before submitting them.
Note 2: A number of Helpful Tips are located at the end of this web page for reference as you complete this exercise.

To FTP your script to the server:

  1. Open Cyberduck (or your preferred FTP client).
  2. Connect to the server using the information we provided to you—host, username, and password. Enter the username (i.e., yourfirstnamelastinitial) and password we provided to you.
  3. Navigate to your publishing folder at studentuploads.ca (e.g., https://www.studentuploads.ca/firstl/202/chapter1). (Replacing firstl with your own userid.)
  4. Upload your script (test.php) to the server. As a rule, most FTP applications save transferred pages to the server with the same filename you gave them on your local computer. If your particular FTP program gives you the option to specify the filename, use test.php.

To test your script in the browser:

  1. Open your favourite web browser.
    For the most part, PHP does not behave differently on different browsers (because PHP runs on the server). HTML, on the other hand, garners inconsistent results on various browsers and operating systems.
  2. Enter the URL location of your script that has been saved.
    Live, from the remote server:
    https://www.studentuploads.ca/firstl/202/chapter1 (Replacing firstl with your own userid.)
  3. From your local computer using XAMPP:
    http://localhost/202/chapter1 (Windows)
    http://localhost/202/chapter1 (Mac)
  4. Add /test.php to the URL.
  5. Press Return.

The page should load in your browser window (Figure 1-3). If you see the PHP code (Figure 1-4) or a blank page, it most likely means that PHP has not been enabled on the server or you are using an unrecognized file extension.

Figure 1-3: If your script has been executed correctly, you should see this result in your browser (woohoo!).

Note: You will see a different version of PHP when you open test.php on your localhost address http://localhost/202/chapter1/test.php (v8.2.4) - seen below, as compared to viewing the test.php file in the browser on the studentuploads.ca remote server (v8.2.17). Even though the PHP version numbers are slightly different, for our purposes here, there is virtually no difference between them aside from bug fixes addressed with v8.2.17.

The executed result in the browser view.

Figure 1-4: If you see the raw PHP, HTML code, or perhaps even a blank page in your browser, then PHP may be installed incorrectly or the extension you used (.php) is not being treated as a PHP file by the server.

The example of displaying PHP code because of the PHP or Server error in the browser view.

If something doesn’t look right in your document, and you need to make changes to the script:

  1. Close the local file in Brackets that you just saved.
  2. Open a connection to the production server using Synapse.
    Synapse showing studentuploads.ca@userid@studentuploads.ca credentials.
    See the Set Up page for a refresher on how to use Synapse.
  3. Once connected with Synapse, you should see the file you just uploaded: test.php. Click on that file to open it. You should now be working with the test.php file that resides on the production server. (Not the one that resides on your local server.)
  4. Make your changes to the code, and save the file by clicking Ctrl-S.
  5. Switch back to your browser, refresh, and view the changes.
  6. Once you have opened the production server version of the file with Synapse, all you have to do for further editing is to repeat this process:
    1. Make your coding changes
    2. Save with Ctrl-S
    3. Switch back to your browser
    4. Refresh and view your changes

 

Quick tips icon.

  • It is important to remember that you cannot open a PHP file directly in your web browser without uploading as you would an HTML page or files in other applications. PHP scripts must be processed by the web server, which means you must access them via a URL (an address that starts with http://).
  • Even if you are not a seasoned computer professional, you should consider installing PHP on your computer.
  • Technically speaking, you do not actually need to add any HTML to a phpinfo() script. It will generate its own proper tags.

Return to Chapter 1, Assignments Page